mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
feat: refactored as_ref_mut.rs to as_ref.rs and as_mut1.rs and add as_mut2.rs
This commit is contained in:
parent
32b234c9f0
commit
4bc0d4b764
24
exercises/conversions/as_mut1.rs
Normal file
24
exercises/conversions/as_mut1.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
41
exercises/conversions/as_mut2.rs
Normal file
41
exercises/conversions/as_mut2.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
// AsRef and AsMut allow for cheap reference-to-reference conversions.
|
// AsRef allows for cheap reference-to-reference conversions.
|
||||||
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
|
// Read more about it 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` or use the `hint` watch subcommand for a hint.
|
||||||
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
@ -17,13 +16,6 @@ fn char_counter<T>(arg: T) -> usize {
|
|||||||
arg.as_ref().chars().count()
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -51,11 +43,4 @@ mod tests {
|
|||||||
let s = String::from("Cafe au lait");
|
let s = String::from("Cafe au lait");
|
||||||
assert_eq!(char_counter(s.clone()), byte_counter(s));
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
23
info.toml
23
info.toml
@ -1154,8 +1154,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?"""
|
Challenge: Can you make the `TryFrom` implementations generic over many integer types?"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "as_ref_mut"
|
name = "as_ref"
|
||||||
path = "exercises/conversions/as_ref_mut.rs"
|
path = "exercises/conversions/as_ref.rs"
|
||||||
mode = "test"
|
mode = "test"
|
||||||
hint = """
|
hint = """
|
||||||
Add AsRef<str> as a trait bound to the functions."""
|
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."""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user