diff --git a/exercises/23_conversions/README.md b/exercises/23_conversions/README.md index fe337331..992676fb 100644 --- a/exercises/23_conversions/README.md +++ b/exercises/23_conversions/README.md @@ -11,6 +11,11 @@ The traits are the following: - `TryFrom` and `TryInto` covered in [`conversions4`](conversions4.rs) - `AsRef` and `AsMut` covered in [`conversions5`](conversions5.rs) +`AsRef` and `AsMut` are useful when a function only needs to borrow a value in +another form. For example, a function accepting `T: AsRef` can work with +both a string slice and an owned `String` without requiring separate +implementations for each type. + Furthermore, the `std::str` module offers a trait called [`FromStr`](https://doc.rust-lang.org/std/str/trait.FromStr.html) which helps with converting strings into target types via the `parse` method on strings. If properly implemented for a given type `Person`, then `let p: Person = "Mark,20".parse().unwrap()` should both compile and run without panicking. These should be the main ways ***within the standard library*** to convert data into your desired types. diff --git a/exercises/23_conversions/conversions5.rs b/exercises/23_conversions/conversions5.rs index d7892dd4..c1537b3c 100644 --- a/exercises/23_conversions/conversions5.rs +++ b/exercises/23_conversions/conversions5.rs @@ -1,22 +1,27 @@ -// 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. +// `AsRef` and `AsMut` let a function accept different input types while +// borrowing the value it needs. For example, both `&str` and `String` can be +// viewed as `&str`, and both `u32` and `Box` can be viewed as `&mut u32`. +// 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. -// Obtain the number of bytes (not characters) in the given argument -// (`.len()` returns the number of bytes in a string). +// Obtain the number of bytes (not characters) in the given argument. The +// `AsRef` bound allows this function to work with both `&str` and `String`. +// (`.len()` returns the number of bytes in a string.) // TODO: Add the `AsRef` trait appropriately as a trait bound. fn byte_counter(arg: T) -> usize { arg.as_ref().len() } -// Obtain the number of characters (not bytes) in the given argument. +// Obtain the number of characters (not bytes) in the given argument. Reuse the +// same `AsRef` bound so this function also accepts `&str` and `String`. // TODO: Add the `AsRef` trait appropriately as a trait bound. fn char_counter(arg: T) -> usize { arg.as_ref().chars().count() } -// Squares a number using `as_mut()`. -// TODO: Add the appropriate trait bound. +// Square a number through a mutable reference. The `AsMut` bound allows +// this function to work with values such as `Box` as well as `u32`. +// TODO: Add the `AsMut` trait bound. fn num_sq(arg: &mut T) { // TODO: Implement the function body. } diff --git a/solutions/23_conversions/conversions5.rs b/solutions/23_conversions/conversions5.rs index a5d2d4fa..cc48e539 100644 --- a/solutions/23_conversions/conversions5.rs +++ b/solutions/23_conversions/conversions5.rs @@ -1,19 +1,24 @@ -// 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. +// `AsRef` and `AsMut` let a function accept different input types while +// borrowing the value it needs. For example, both `&str` and `String` can be +// viewed as `&str`, and both `u32` and `Box` can be viewed as `&mut u32`. +// 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. -// Obtain the number of bytes (not characters) in the given argument -// (`.len()` returns the number of bytes in a string). +// Obtain the number of bytes (not characters) in the given argument. The +// `AsRef` bound allows this function to work with both `&str` and `String`. +// (`.len()` returns the number of bytes in a string.) fn byte_counter>(arg: T) -> usize { arg.as_ref().len() } -// Obtain the number of characters (not bytes) in the given argument. +// Obtain the number of characters (not bytes) in the given argument. Reuse the +// same `AsRef` bound so this function also accepts `&str` and `String`. fn char_counter>(arg: T) -> usize { arg.as_ref().chars().count() } -// Squares a number using `as_mut()`. +// Square a number through a mutable reference. The `AsMut` bound allows +// this function to work with values such as `Box` as well as `u32`. fn num_sq>(arg: &mut T) { let arg = arg.as_mut(); *arg *= *arg;