diff --git a/exercises/conversions/as_mut1.rs b/exercises/conversions/as_mut1.rs new file mode 100644 index 0000000000..8ab594908e --- /dev/null +++ b/exercises/conversions/as_mut1.rs @@ -0,0 +1,24 @@ +// AsMut allows for cheap mutable reference-to-reference conversions. +// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html. +// Execute `rustlings hint as_mut1` or use the `hint` watch subcommand for a hint. + +// I AM NOT DONE + +// Squares a number using as_mut(). +// TODO: Add the appropriate trait bound. +fn num_sq(arg: &mut T) { + // TODO: Implement the function's body. + ??? +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn mult_box() { + let mut num: Box = Box::new(3); + num_sq(&mut num); + assert_eq!(*num, 9); + } +} diff --git a/exercises/conversions/as_mut2.rs b/exercises/conversions/as_mut2.rs new file mode 100644 index 0000000000..3696c496b3 --- /dev/null +++ b/exercises/conversions/as_mut2.rs @@ -0,0 +1,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 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(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 = Box::new(3); + num_sq(&mut num); + assert_eq!(*num, 9); + } + + #[test] + fn mult_box_f32() { + let mut num: Box = Box::new(3.0); + num_sq(&mut num); + assert_eq!(*num, 9.0); + } +} diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref.rs similarity index 63% rename from exercises/conversions/as_ref_mut.rs rename to exercises/conversions/as_ref.rs index e6a9d11472..023385739c 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref.rs @@ -1,7 +1,6 @@ -// 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 -// 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. +// AsRef allows for cheap reference-to-reference conversions. +// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsRef.html +// Execute `rustlings hint as_ref` or use the `hint` watch subcommand for a hint. // I AM NOT DONE @@ -17,13 +16,6 @@ fn char_counter(arg: T) -> usize { arg.as_ref().chars().count() } -// Squares a number using as_mut(). -// TODO: Add the appropriate trait bound. -fn num_sq(arg: &mut T) { - // TODO: Implement the function body. - ??? -} - #[cfg(test)] mod tests { use super::*; @@ -51,11 +43,4 @@ mod tests { let s = String::from("Cafe au lait"); assert_eq!(char_counter(s.clone()), byte_counter(s)); } - - #[test] - fn mult_box() { - let mut num: Box = Box::new(3); - num_sq(&mut num); - assert_eq!(*num, 9); - } } diff --git a/info.toml b/info.toml index 4b87819303..75ccdc5a1a 100644 --- a/info.toml +++ b/info.toml @@ -1154,8 +1154,27 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen Challenge: Can you make the `TryFrom` implementations generic over many integer types?""" [[exercises]] -name = "as_ref_mut" -path = "exercises/conversions/as_ref_mut.rs" +name = "as_ref" +path = "exercises/conversions/as_ref.rs" mode = "test" hint = """ Add AsRef as a trait bound to the functions.""" + +[[exercises]] +name = "as_mut1" +path = "exercises/conversions/as_mut1.rs" +mode = "test" +hint = """ +Add AsMut as a trait bound to the function.""" + +[[exercises]] +name = "as_mut2" +path = "exercises/conversions/as_mut2.rs" +mode = "test" +hint = """ +Now we need to tell the compiler about two types. Type T is Box, while +type U is a number. Numbers can implement std::ops::MulAssign (more at +https://doc.rust-lang.org/std/ops/trait.MulAssign.html). The number +might also need to be copyable. + +You might need to use a temporary variable in the function's body."""