feat(as_ref_mut): add AsMut section

This commit is contained in:
mokou 2022-07-15 12:50:01 +02:00
parent 81d25aecff
commit 4bebdb5f02
1 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,7 @@
// AsRef and AsMut allow for cheap reference-to-reference conversions. // AsRef and AsMut allow for cheap reference-to-reference conversions.
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html // Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively. // and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM NOT DONE
@ -16,10 +17,10 @@ fn char_counter<T>(arg: T) -> usize {
arg.as_ref().chars().count() arg.as_ref().chars().count()
} }
fn main() { // Squares a number using AsMut. Add the trait bound as is appropriate and
let s = "Café au lait"; // implement the function body.
println!("{}", char_counter(s)); fn num_sq<T>(arg: &mut T) {
println!("{}", byte_counter(s)); ???
} }
#[cfg(test)] #[cfg(test)]
@ -49,4 +50,11 @@ mod tests {
let s = String::from("Cafe au lait"); let s = String::from("Cafe au lait");
assert_eq!(char_counter(s.clone()), byte_counter(s)); assert_eq!(char_counter(s.clone()), byte_counter(s));
} }
#[test]
fn mult_box() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
} }