Skip to content

Commit abc3013

Browse files
authored
Merge pull request #1660 from rust-lang/fix/move-semantics-tests
fix: refactor move semantics 1-4 into tests
2 parents 6eb9bde + 51e237d commit abc3013

File tree

5 files changed

+27
-51
lines changed

5 files changed

+27
-51
lines changed

exercises/move_semantics/move_semantics1.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@
55

66
// I AM NOT DONE
77

8+
#[test]
89
fn main() {
9-
let vec0 = Vec::new();
10+
let vec0 = vec![22, 44, 66];
1011

1112
let vec1 = fill_vec(vec0);
1213

13-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
14-
15-
vec1.push(88);
16-
17-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
14+
assert_eq!(vec1, vec![22, 44, 66, 88]);
1815
}
1916

2017
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
21-
let mut vec = vec;
18+
let vec = vec;
2219

23-
vec.push(22);
24-
vec.push(44);
25-
vec.push(66);
20+
vec.push(88);
2621

2722
vec
2823
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
// move_semantics2.rs
22
//
3-
// Expected output:
4-
// vec0 has length 3, with contents `[22, 44, 66]`
5-
// vec1 has length 4, with contents `[22, 44, 66, 88]`
3+
// Make the test pass by finding a way to keep both Vecs separate!
64
//
75
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
86
// for a hint.
97

108
// I AM NOT DONE
119

10+
#[test]
1211
fn main() {
13-
let vec0 = Vec::new();
12+
let vec0 = vec![22, 44, 66];
1413

1514
let mut vec1 = fill_vec(vec0);
1615

17-
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
18-
19-
vec1.push(88);
20-
21-
println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1);
16+
assert_eq!(vec0, vec![22, 44, 66]);
17+
assert_eq!(vec1, vec![22, 44, 66, 88]);
2218
}
2319

2420
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
2521
let mut vec = vec;
2622

27-
vec.push(22);
28-
vec.push(44);
29-
vec.push(66);
23+
vec.push(88);
3024

3125
vec
3226
}

exercises/move_semantics/move_semantics3.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,17 @@
88

99
// I AM NOT DONE
1010

11+
#[test]
1112
fn main() {
12-
let vec0 = Vec::new();
13+
let vec0 = vec![22, 44, 66];
1314

1415
let mut vec1 = fill_vec(vec0);
1516

16-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
17-
18-
vec1.push(88);
19-
20-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
17+
assert_eq!(vec1, vec![22, 44, 66, 88]);
2118
}
2219

2320
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
24-
vec.push(22);
25-
vec.push(44);
26-
vec.push(66);
21+
vec.push(88);
2722

2823
vec
2924
}

exercises/move_semantics/move_semantics4.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,21 @@
99

1010
// I AM NOT DONE
1111

12+
#[test]
1213
fn main() {
13-
let vec0 = Vec::new();
14+
let vec0 = vec![22, 44, 66];
1415

1516
let mut vec1 = fill_vec(vec0);
1617

17-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
18-
19-
vec1.push(88);
20-
21-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
18+
assert_eq!(vec1, vec![22, 44, 66, 88]);
2219
}
2320

24-
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
21+
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
2522
fn fill_vec() -> Vec<i32> {
23+
// Instead, let's create and fill the Vec in here - how do you do that?
2624
let mut vec = vec;
2725

28-
vec.push(22);
29-
vec.push(44);
30-
vec.push(66);
26+
vec.push(88);
3127

3228
vec
3329
}

info.toml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ better. What do you think is the more commonly used pattern under Rust developer
284284
[[exercises]]
285285
name = "move_semantics1"
286286
path = "exercises/move_semantics/move_semantics1.rs"
287-
mode = "compile"
287+
mode = "test"
288288
hint = """
289-
So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on the line
289+
So you've got the "cannot borrow immutable local variable `vec` as mutable" error on the line
290290
where we push an element to the vector, right?
291291
The fix for this is going to be adding one keyword, and the addition is NOT on the line where
292292
we push to the vector (where the error is).
@@ -296,7 +296,7 @@ Also: Try accessing `vec0` after having called `fill_vec()`. See what happens!""
296296
[[exercises]]
297297
name = "move_semantics2"
298298
path = "exercises/move_semantics/move_semantics2.rs"
299-
mode = "compile"
299+
mode = "test"
300300
hint = """
301301
When running this exercise for the first time, you'll notice an error about
302302
"borrow of moved value". In Rust, when an argument is passed to a function and
@@ -309,16 +309,12 @@ Rust provides a couple of different ways to mitigate this issue, feel free to tr
309309
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
310310
and then copy the data within the function (`vec.clone()`) in order to return an owned
311311
`Vec<i32>`.
312-
3. Or, you could make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
313-
mutable), modify it directly, then not return anything. This means that `vec0` will change over the
314-
course of the function, and makes `vec1` redundant (make sure to change the parameters of the `println!`
315-
statements if you go this route)
316312
"""
317313

318314
[[exercises]]
319315
name = "move_semantics3"
320316
path = "exercises/move_semantics/move_semantics3.rs"
321-
mode = "compile"
317+
mode = "test"
322318
hint = """
323319
The difference between this one and the previous ones is that the first line
324320
of `fn fill_vec` that had `let mut vec = vec;` is no longer there. You can,
@@ -328,7 +324,7 @@ an existing binding to be a mutable binding instead of an immutable one :)"""
328324
[[exercises]]
329325
name = "move_semantics4"
330326
path = "exercises/move_semantics/move_semantics4.rs"
331-
mode = "compile"
327+
mode = "test"
332328
hint = """
333329
Stop reading whenever you feel like you have enough direction :) Or try
334330
doing one step and then fixing the compiler errors that result!
@@ -337,7 +333,7 @@ So the end goal is to:
337333
- so then `vec0` doesn't exist, so we can't pass it to `fill_vec`
338334
- `fill_vec` has had its signature changed, which our call should reflect
339335
- since we're not creating a new vec in `main` anymore, we need to create
340-
a new vec in `fill_vec`, similarly to the way we did in `main`"""
336+
a new vec in `fill_vec`, and fill it with the expected values"""
341337

342338
[[exercises]]
343339
name = "move_semantics5"

0 commit comments

Comments
 (0)