I'm working my way through Rustlings 5.2.1 in conversions/as_mut_ref.rs, so
Spoiler Alert!
The current version of this exercise asks us to add a trait bound and implement a function that takes a mutable reference to a number and squares the number in-place. Here's the test that runs the function num_sq():
It seems like I somehow need to tell the compiler "here's how to handle Box and here's how to square T assuming it supports multiplication and assignment," but I'm not sure how to express that in Rust's syntax.
Is there a way to generalize the generic num_sq()?
T: AsMut<T> is your problem. In your concrete test T is Box, which doesn't have anything to do with multiplication. You need another type parameter to represent the numeric type
I ended up with this, using Mul and no temporary variable.
fn num_sq<T, U>(arg: &mut T)
where
T: AsMut<U>,
U: std::ops::Mul<Output = U> + Copy,
{
// as_mut will give a ref if mut is not needed
// then we can use it twice in a block
// the references are released before
// the as_mut mutable reference is used.
*arg.as_mut() = {
*arg.as_mut() * *arg.as_mut()
}
}
Just because I wanted to do it with Mul, not MulAssign
Thanks! I was curious how the compiler would handle the two different implementations. The unoptimized assembly language is pretty similar, and basically what you'd expect: load parameters and then either call <u32 as core::ops::arith::MulAssign>::mul_assign or <u32 as core::ops::arith::Mul>::mul followed by a manual assignment.