Skip to content

Commit d8969cd

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

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

exercises/14_generics/generics3.rs

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

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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::iter::Sum;
2+
use std::num::TryFrom;
3+
use std::ops::Div;
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 .map() 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 as TryFrom<usize>>::Error>
11+
where
12+
T: Div<Output = T> + Sum + TryFrom<usize> + Copy,
13+
{
14+
Ok(input.iter().map(|x| *x).sum::<T>() / T::try_from(input.len())?)
15+
}
16+
17+
fn main() {
18+
// You can optionally experiment here.
19+
}
20+
21+
//you may add `.unwrap()` to the avg fuction calls if needed
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_u8() {
28+
let input: [u8; 5] = [2, 4, 6, 8, 10];
29+
let ans: u8 = avg(&input).unwrap();
30+
assert_eq!(ans, 6);
31+
}
32+
33+
fn test_f32() {
34+
let input: [i32; 5] = [546, 263, 8764, 4198, 7654];
35+
let ans: i32 = avg(&input).unwrap();
36+
assert_eq!(ans, 4285);
37+
}
38+
}

0 commit comments

Comments
 (0)