-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve hint for as_ref_mut #1113
Conversation
@@ -1135,4 +1135,4 @@ name = "as_ref_mut" | |||
path = "exercises/conversions/as_ref_mut.rs" | |||
mode = "test" | |||
hint = """ | |||
Add AsRef<str> as a trait bound to the functions.""" | |||
Add AsRef<str> or AsMut<u32> as a trait bound to the functions.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add AsRef<str> or AsMut<u32> as a trait bound to the functions.""" | |
Add AsRef<str> or AsMut<u32> as a trait bound to the functions as appropriate.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you limit the trait to u32? Shouldn't the trait allow any parameter that can be squared, so Mul trait? I know that the unit test specifies u32, but that seems pretty narrow, shouldn't the function support i32, i64, u32, u64, f32, f64?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just as an example 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can actually make it generic, e.g.:
fn num_sq<T: AsMut<U> + AsRef<U>, U>(arg: &mut T) where U: Copy + MulAssign<U> {
let x = *arg.as_ref();
*arg.as_mut() *= x;
}
Just realised this existed and went to put in my own PR instead (That I've now closed) - @Frosthage Instead of straight up adding the answer there (and also limiting it to a u32, like @CaliViking mentioned above), what if it had it's own hint saying something like "Consider the type you'd want to use" - I think it's already clear (ish) from the actual comments on that file that it wants to be |
FWIW, I just went through all the exercises (as a genuine newbie to Rust) and this was the only exercise I failed to complete properly (as far as I can tell), and I found the hint quite unhelpful. I could fix it if I specialised |
Great improvement for sure! |
@all-contributors please add @Frosthage for content |
I've put up a pull request to add @Frosthage! 🎉 |
The hint for as_ref_mut was written before the function
num_sq
was added and is currently incorrect. I'm pretty sure you can improve this hint, but at least it's correct