You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for your excellent work on the Rustlings exercises. Most of the tasks were easy to follow and had a clear purpose. However, I found myself completely stumped by the as_ref_mut exercise, as I had no idea what I was supposed to do or why I would use this language construct at all.
Initially, I thought the goal was to write my own AsRef implementation for some sort of generic type or even for str itself. Neither reading the documentation nor searching for additional information helped to clarify this. In the end, I had to resort to looking at the solution files to figure out what to do. 😞
I humbly suggest the following improvements for the as_ref_mut exercise:
Provide a bit more background on why AsRef is needed and how it is commonly used.
Use more practical example where AsRef really adds significant value.
Let the user implement their own AsRef implementation for a type as a learning exercise.
Consider removing the generics, as it was a bit distracting (at least for me).
Thank you again for your hard work on this project!
The text was updated successfully, but these errors were encountered:
I agree that it could use a bit more clarification, especially since the whole unit is not covered in the book and we just get the std lib docs as reference in the readme.
The Generics are needed though as I think you have to add the as_mut bound in all three functions plus a little impl in the last fn.
My solution looks like this (note that I'm a rust novice so there might be better ways of doing this):
// Obtain the number of bytes (not characters) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound.
fn byte_counter<T: AsRef<str>> (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<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().chars().count()
}
// Squares a number using as_mut().
// TODO: Add the appropriate trait bound.
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
let x = arg.as_mut();
let v = *x;
*x = v * v;
}
I'd like to add something to this it should be statet that this is only required to fit for an u32 as i was confused with that and ended up implementing something like this
// Squares a number using `as_mut()`.// TODO: Add the appropriate trait bound.fnnum_sq<Y:Mul<Output = Y> + Copy,T:AsMut<Y>>(arg:&mutT){// TODO: Implement the function body.let arg = arg.as_mut();*arg = *arg **arg;}
Hi!
Thank you for your excellent work on the Rustlings exercises. Most of the tasks were easy to follow and had a clear purpose. However, I found myself completely stumped by the
as_ref_mut
exercise, as I had no idea what I was supposed to do or why I would use this language construct at all.Initially, I thought the goal was to write my own
AsRef
implementation for some sort of generic type or even forstr
itself. Neither reading the documentation nor searching for additional information helped to clarify this. In the end, I had to resort to looking at the solution files to figure out what to do. 😞I humbly suggest the following improvements for the
as_ref_mut
exercise:AsRef
is needed and how it is commonly used.AsRef
really adds significant value.AsRef
implementation for a type as a learning exercise.Thank you again for your hard work on this project!
The text was updated successfully, but these errors were encountered: