Skip to content

Commit 4bc0d4b

Browse files
author
Jeff Cours
committed
feat: refactored as_ref_mut.rs to as_ref.rs and as_mut1.rs and add as_mut2.rs
1 parent 32b234c commit 4bc0d4b

File tree

4 files changed

+89
-20
lines changed

4 files changed

+89
-20
lines changed

exercises/conversions/as_mut1.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// AsMut allows for cheap mutable reference-to-reference conversions.
2+
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
3+
// Execute `rustlings hint as_mut1` or use the `hint` watch subcommand for a hint.
4+
5+
// I AM NOT DONE
6+
7+
// Squares a number using as_mut().
8+
// TODO: Add the appropriate trait bound.
9+
fn num_sq<T>(arg: &mut T) {
10+
// TODO: Implement the function's body.
11+
???
12+
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
use super::*;
17+
18+
#[test]
19+
fn mult_box() {
20+
let mut num: Box<u32> = Box::new(3);
21+
num_sq(&mut num);
22+
assert_eq!(*num, 9);
23+
}
24+
}

exercises/conversions/as_mut2.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// AsMut allows for cheap reference-to-reference conversions.
2+
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsMut.html.
3+
//
4+
// In conversions/as_mut1.rs, we implemented a function that would square a
5+
// Box<u32> in-place using as_mut(). Now we're going to generalize the function
6+
// to work with a Box containing any numeric type that supports multiplication
7+
// and assignment.
8+
//
9+
// Execute `rustlings hint as_mut2` or use the `hint` watch subcommand for a hint.
10+
11+
// I AM NOT DONE
12+
13+
// Squares a number using as_mut().
14+
// TODO: Add the appropriate trait bounds.
15+
fn num_sq<T, U>(arg: &mut T)
16+
where
17+
T: ???,
18+
U: ???,
19+
{
20+
// TODO: Implement the function's body.
21+
???
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn mult_box_u32() {
30+
let mut num: Box<u32> = Box::new(3);
31+
num_sq(&mut num);
32+
assert_eq!(*num, 9);
33+
}
34+
35+
#[test]
36+
fn mult_box_f32() {
37+
let mut num: Box<f32> = Box::new(3.0);
38+
num_sq(&mut num);
39+
assert_eq!(*num, 9.0);
40+
}
41+
}

exercises/conversions/as_ref_mut.rs exercises/conversions/as_ref.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// AsRef and AsMut allow for cheap reference-to-reference conversions.
2-
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
3-
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
4-
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
1+
// AsRef allows for cheap reference-to-reference conversions.
2+
// Read more about it at https://doc.rust-lang.org/std/convert/trait.AsRef.html
3+
// Execute `rustlings hint as_ref` or use the `hint` watch subcommand for a hint.
54

65
// I AM NOT DONE
76

@@ -17,13 +16,6 @@ fn char_counter<T>(arg: T) -> usize {
1716
arg.as_ref().chars().count()
1817
}
1918

20-
// Squares a number using as_mut().
21-
// TODO: Add the appropriate trait bound.
22-
fn num_sq<T>(arg: &mut T) {
23-
// TODO: Implement the function body.
24-
???
25-
}
26-
2719
#[cfg(test)]
2820
mod tests {
2921
use super::*;
@@ -51,11 +43,4 @@ mod tests {
5143
let s = String::from("Cafe au lait");
5244
assert_eq!(char_counter(s.clone()), byte_counter(s));
5345
}
54-
55-
#[test]
56-
fn mult_box() {
57-
let mut num: Box<u32> = Box::new(3);
58-
num_sq(&mut num);
59-
assert_eq!(*num, 9);
60-
}
6146
}

info.toml

+21-2
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,27 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
11541154
Challenge: Can you make the `TryFrom` implementations generic over many integer types?"""
11551155

11561156
[[exercises]]
1157-
name = "as_ref_mut"
1158-
path = "exercises/conversions/as_ref_mut.rs"
1157+
name = "as_ref"
1158+
path = "exercises/conversions/as_ref.rs"
11591159
mode = "test"
11601160
hint = """
11611161
Add AsRef<str> as a trait bound to the functions."""
1162+
1163+
[[exercises]]
1164+
name = "as_mut1"
1165+
path = "exercises/conversions/as_mut1.rs"
1166+
mode = "test"
1167+
hint = """
1168+
Add AsMut<u32> as a trait bound to the function."""
1169+
1170+
[[exercises]]
1171+
name = "as_mut2"
1172+
path = "exercises/conversions/as_mut2.rs"
1173+
mode = "test"
1174+
hint = """
1175+
Now we need to tell the compiler about two types. Type T is Box<U>, while
1176+
type U is a number. Numbers can implement std::ops::MulAssign (more at
1177+
https://doc.rust-lang.org/std/ops/trait.MulAssign.html). The number
1178+
might also need to be copyable.
1179+
1180+
You might need to use a temporary variable in the function's body."""

0 commit comments

Comments
 (0)