Skip to content

Commit 369ae2e

Browse files
author
liv
committed
feat(move_semantics2): rewrite hint
1 parent 1ce9d93 commit 369ae2e

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

exercises/move_semantics/move_semantics2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
33

44
// Expected output:
5-
// vec0 has length 3 content `[22, 44, 66]`
6-
// vec1 has length 4 content `[22, 44, 66, 88]`
5+
// vec0 has length 3, with contents `[22, 44, 66]`
6+
// vec1 has length 4, with contents `[22, 44, 66, 88]`
77

88
// I AM NOT DONE
99

1010
fn main() {
1111
let vec0 = Vec::new();
1212

13-
// Do not move the following line!
1413
let mut vec1 = fill_vec(vec0);
1514

16-
// Do not change the following line!
17-
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
15+
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
1816

1917
vec1.push(88);
2018

21-
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
19+
println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1);
2220
}
2321

2422
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {

info.toml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,24 @@ Also: Try accessing `vec0` after having called `fill_vec()`. See what happens!""
287287
[[exercises]]
288288
name = "move_semantics2"
289289
path = "exercises/move_semantics/move_semantics2.rs"
290-
mode = "compile"
290+
mode = "test"
291291
hint = """
292-
So, `vec0` is passed into the `fill_vec` function as an argument. In Rust,
293-
when an argument is passed to a function and it's not explicitly returned,
294-
you can't use the original variable anymore. We call this "moving" a variable.
295-
Variables that are moved into a function (or block scope) and aren't explicitly
296-
returned get "dropped" at the end of that function. This is also what happens here.
297-
There's a few ways to fix this, try them all if you want:
298-
1. Make another, separate version of the data that's in `vec0` and pass that
292+
When running this exercise for the first time, you'll notice an error about
293+
"borrow of moved value". In Rust, when an argument is passed to a function and
294+
it's not explicitly returned, you can't use the original variable anymore.
295+
We call this "moving" a variable. When we pass `vec0` into `fill_vec`, it's being
296+
"moved" into `vec1`, meaning we can't access `vec0` anymore after the fact.
297+
Rust provides a couple of different ways to mitigate this issue, feel free to try them all:
298+
1. You could make another, separate version of the data that's in `vec0` and pass that
299299
to `fill_vec` instead.
300300
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
301-
and then copy the data within the function in order to return an owned
302-
`Vec<i32>`
303-
3. Make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
304-
mutable), modify it directly, then not return anything. Then you can get rid
305-
of `vec1` entirely -- note that this will change what gets printed by the
306-
first `println!`"""
301+
and then copy the data within the function (`vec.clone()`) in order to return an owned
302+
`Vec<i32>`.
303+
3. Or, you could make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
304+
mutable), modify it directly, then not return anything. This means that `vec0` will change over the
305+
course of the function, and makes `vec1` redundant (make sure to change the parameters of the `println!`
306+
statements if you go this route)
307+
"""
307308

308309
[[exercises]]
309310
name = "move_semantics3"

0 commit comments

Comments
 (0)