-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathas_mut2.rs
41 lines (36 loc) · 1.01 KB
/
as_mut2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// AsMut allows for cheap reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
//
// In conversions/as_mut1.rs, we implemented a function that would square a
// Box<u32> in-place using as_mut(). Now we're going to generalize the function
// to work with a Box containing any numeric type that supports multiplication
// and assignment.
//
// Execute `rustlings hint as_mut2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
// Squares a number using as_mut().
// TODO: Add the appropriate trait bounds.
fn num_sq<T, U>(arg: &mut T)
where
T: ???,
U: ???,
{
// TODO: Implement the function's body.
???
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mult_box_u32() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
#[test]
fn mult_box_f32() {
let mut num: Box<f32> = Box::new(3.0);
num_sq(&mut num);
assert_eq!(*num, 9.0);
}
}