Merge 4bc0d4b76414bcd43b24088f34b624a11b80d210 into 30291a3c253f08a4191cfd544ba36867612ebb7a

This commit is contained in:
jtcours 2023-05-24 16:47:42 +09:00 committed by GitHub
commit cb9d5c2200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 20 deletions

View File

@ -0,0 +1,24 @@
// AsMut allows for cheap mutable reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
// Execute `rustlings hint as_mut1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
// Squares a number using as_mut().
// TODO: Add the appropriate trait bound.
fn num_sq<T>(arg: &mut T) {
// TODO: Implement the function's body.
???
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mult_box() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
}

View File

@ -0,0 +1,41 @@
// AsMut allows for cheap reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
//
// In conversions/as_mut1.rs, we implemented a function that would square a
// Box<u32> in-place using as_mut(). Now we're going to generalize the function
// to work with a Box containing any numeric type that supports multiplication
// and assignment.
//
// Execute `rustlings hint as_mut2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
// Squares a number using as_mut().
// TODO: Add the appropriate trait bounds.
fn num_sq<T, U>(arg: &mut T)
where
T: ???,
U: ???,
{
// TODO: Implement the function's body.
???
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mult_box_u32() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
#[test]
fn mult_box_f32() {
let mut num: Box<f32> = Box::new(3.0);
num_sq(&mut num);
assert_eq!(*num, 9.0);
}
}

View File

@ -1,7 +1,6 @@
// 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.
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
// AsRef allows for cheap reference-to-reference conversions.
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsRef.html
// Execute `rustlings hint as_ref` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@ -17,13 +16,6 @@ fn char_counter<T>(arg: T) -> usize {
arg.as_ref().chars().count()
}
// Squares a number using as_mut().
// TODO: Add the appropriate trait bound.
fn num_sq<T>(arg: &mut T) {
// TODO: Implement the function body.
???
}
#[cfg(test)]
mod tests {
use super::*;
@ -51,11 +43,4 @@ mod tests {
let s = String::from("Cafe au lait");
assert_eq!(char_counter(s.clone()), byte_counter(s));
}
#[test]
fn mult_box() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
}

View File

@ -1166,8 +1166,27 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
Challenge: Can you make the `TryFrom` implementations generic over many integer types?"""
[[exercises]]
name = "as_ref_mut"
path = "exercises/conversions/as_ref_mut.rs"
name = "as_ref"
path = "exercises/conversions/as_ref.rs"
mode = "test"
hint = """
Add AsRef<str> as a trait bound to the functions."""
[[exercises]]
name = "as_mut1"
path = "exercises/conversions/as_mut1.rs"
mode = "test"
hint = """
Add AsMut<u32> as a trait bound to the function."""
[[exercises]]
name = "as_mut2"
path = "exercises/conversions/as_mut2.rs"
mode = "test"
hint = """
Now we need to tell the compiler about two types. Type T is Box<U>, while
type U is a number. Numbers can implement std::ops::MulAssign (more at
https://doc.rust-lang.org/std/ops/trait.MulAssign.html). The number
might also need to be copyable.
You might need to use a temporary variable in the function's body."""