Skip to content

Commit 1349476

Browse files
committed
feat: add generics3
1 parent b5d440f commit 1349476

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

dev/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ bin = [
116116
{ name = "generics1_sol", path = "../solutions/14_generics/generics1.rs" },
117117
{ name = "generics2", path = "../exercises/14_generics/generics2.rs" },
118118
{ name = "generics2_sol", path = "../solutions/14_generics/generics2.rs" },
119+
{ name = "generics3", path = "../exercises/14_generics/generics3.rs" },
120+
{ name = "generics3_sol", path = "../solutions/14_generics/generics3.rs" },
119121
{ name = "traits1", path = "../exercises/15_traits/traits1.rs" },
120122
{ name = "traits1_sol", path = "../solutions/15_traits/traits1.rs" },
121123
{ name = "traits2", path = "../exercises/15_traits/traits2.rs" },

exercises/14_generics/generics3.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//fn avg<???>(???) -> ???
2+
// TODO: write a fuction that takes in a slice of number-like primitives, eg u8, i16, usize
3+
// and returns the mean of the slice
4+
// you do not to implement this for floats due to a language limitation
5+
6+
fn main() {
7+
// You can optionally experiment here.
8+
}
9+
10+
//you may add `.unwrap()` to the avg fuction calls if needed
11+
#[cfg(test)]
12+
mod tests {
13+
use super::*;
14+
15+
#[test]
16+
fn test_u8() {
17+
let input: [u8; 5] = [2, 4, 6, 8, 10];
18+
let ans: u8 = avg(&input);
19+
assert_eq!(ans, 6);
20+
}
21+
22+
fn test_i32() {
23+
let input: [i32; 5] = [546, 263, 8764, 4198, 7654];
24+
let ans: i32 = avg(&input);
25+
assert_eq!(ans, 4285);
26+
}
27+
}

rustlings-macros/info.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,20 @@ hint = """
736736
Related section in The Book:
737737
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-method-definitions"""
738738

739+
[[exercises]]
740+
name = "generics3"
741+
dir = "14_generics"
742+
hint = """
743+
The fuction should have the required traits for the division,
744+
eg Div to divide the total and size of slice, Sum to sum over the slice
745+
and TryFrom as `as T` is disallowed in generic contexs
746+
747+
You may find the following links usefull:
748+
https://doc.rust-lang.org/std/ops/trait.Div.html
749+
https://doc.rust-lang.org/std/iter/trait.Sum.html
750+
https://doc.rust-lang.org/std/convert/trait.TryFrom.html
751+
"""
752+
739753
# TRAITS
740754

741755
[[exercises]]

solutions/14_generics/generics3.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::iter::Sum;
2+
use std::ops::Div;
3+
use std::convert::TryFrom;
4+
5+
//use Div as we need to div sum by size of slice &[T]
6+
//Use Sum so that .sum() can be used
7+
//Use Copy as .copied() requires it
8+
//use TryFrom to use T::try_from as `as T` is not allowed
9+
10+
fn avg<T>(input: &[T]) -> Result<T, T::Error>
11+
where
12+
T: Div<Output = T> + Sum + TryFrom<usize> + Copy,
13+
{
14+
Ok(input.iter().copied().sum::<T>() / T::try_from(input.len())?)
15+
}
16+
fn main() {
17+
// You can optionally experiment here.
18+
}
19+
20+
//you may add `.unwrap()` to the avg fuction calls if needed
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn test_u8() {
27+
let input: [u8; 5] = [2, 4, 6, 8, 10];
28+
let ans: u8 = avg(&input).unwrap();
29+
assert_eq!(ans, 6);
30+
}
31+
32+
fn test_i32() {
33+
let input: [i32; 5] = [546, 263, 8764, 4198, 7654];
34+
let ans: i32 = avg(&input).unwrap();
35+
assert_eq!(ans, 4285);
36+
}
37+
}

0 commit comments

Comments
 (0)