diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index df626e8b..cf1d56e0 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -9,23 +9,31 @@ // I AM NOT DONE +use std::fmt::Debug; + // Obtain the number of bytes (not characters) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn byte_counter(arg: T) -> usize { +fn byte_counter>(arg: T) -> usize { arg.as_ref().as_bytes().len() } // Obtain the number of characters (not bytes) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn char_counter(arg: T) -> usize { +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) { +// First version that works for mut ref of a num, but it doesn't work if the num is inside something like Box +// fn num_sq + AsMut>(arg: &mut T) { +fn num_sq(arg: &mut T) +where + T : AsMut + std::fmt::Debug, // generalised version where T is a generic container and U is the numeric type + U : std::ops::Mul + Copy +{ // TODO: Implement the function body. - arg = arg * arg; + *arg.as_mut() = *arg.as_mut() * *arg.as_mut(); } #[cfg(test)]