File tree 4 files changed +89
-20
lines changed
4 files changed +89
-20
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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.
5
4
6
5
// I AM NOT DONE
7
6
@@ -17,13 +16,6 @@ fn char_counter<T>(arg: T) -> usize {
17
16
arg. as_ref ( ) . chars ( ) . count ( )
18
17
}
19
18
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
-
27
19
#[ cfg( test) ]
28
20
mod tests {
29
21
use super :: * ;
@@ -51,11 +43,4 @@ mod tests {
51
43
let s = String :: from ( "Cafe au lait" ) ;
52
44
assert_eq ! ( char_counter( s. clone( ) ) , byte_counter( s) ) ;
53
45
}
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
- }
61
46
}
Original file line number Diff line number Diff line change @@ -1154,8 +1154,27 @@ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reen
1154
1154
Challenge: Can you make the `TryFrom` implementations generic over many integer types?"""
1155
1155
1156
1156
[[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"
1159
1159
mode = " test"
1160
1160
hint = """
1161
1161
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."""
You can’t perform that action at this time.
0 commit comments