mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 23:36:30 +00:00
feat(move_semantics2): rewrite hint
This commit is contained in:
parent
1ce9d93e94
commit
369ae2e63d
2 changed files with 19 additions and 20 deletions
|
@ -2,23 +2,21 @@
|
||||||
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// Expected output:
|
// Expected output:
|
||||||
// vec0 has length 3 content `[22, 44, 66]`
|
// vec0 has length 3, with contents `[22, 44, 66]`
|
||||||
// vec1 has length 4 content `[22, 44, 66, 88]`
|
// vec1 has length 4, with contents `[22, 44, 66, 88]`
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = Vec::new();
|
||||||
|
|
||||||
// Do not move the following line!
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
// Do not change the following line!
|
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
|
||||||
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
|
|
||||||
|
|
||||||
vec1.push(88);
|
vec1.push(88);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
|
|
29
info.toml
29
info.toml
|
@ -287,23 +287,24 @@ Also: Try accessing `vec0` after having called `fill_vec()`. See what happens!""
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "move_semantics2"
|
name = "move_semantics2"
|
||||||
path = "exercises/move_semantics/move_semantics2.rs"
|
path = "exercises/move_semantics/move_semantics2.rs"
|
||||||
mode = "compile"
|
mode = "test"
|
||||||
hint = """
|
hint = """
|
||||||
So, `vec0` is passed into the `fill_vec` function as an argument. In Rust,
|
When running this exercise for the first time, you'll notice an error about
|
||||||
when an argument is passed to a function and it's not explicitly returned,
|
"borrow of moved value". In Rust, when an argument is passed to a function and
|
||||||
you can't use the original variable anymore. We call this "moving" a variable.
|
it's not explicitly returned, you can't use the original variable anymore.
|
||||||
Variables that are moved into a function (or block scope) and aren't explicitly
|
We call this "moving" a variable. When we pass `vec0` into `fill_vec`, it's being
|
||||||
returned get "dropped" at the end of that function. This is also what happens here.
|
"moved" into `vec1`, meaning we can't access `vec0` anymore after the fact.
|
||||||
There's a few ways to fix this, try them all if you want:
|
Rust provides a couple of different ways to mitigate this issue, feel free to try them all:
|
||||||
1. Make another, separate version of the data that's in `vec0` and pass that
|
1. You could make another, separate version of the data that's in `vec0` and pass that
|
||||||
to `fill_vec` instead.
|
to `fill_vec` instead.
|
||||||
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
|
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
|
||||||
and then copy the data within the function in order to return an owned
|
and then copy the data within the function (`vec.clone()`) in order to return an owned
|
||||||
`Vec<i32>`
|
`Vec<i32>`.
|
||||||
3. Make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
|
3. Or, you could make `fill_vec` *mutably* borrow a reference to its argument (which will need to be
|
||||||
mutable), modify it directly, then not return anything. Then you can get rid
|
mutable), modify it directly, then not return anything. This means that `vec0` will change over the
|
||||||
of `vec1` entirely -- note that this will change what gets printed by the
|
course of the function, and makes `vec1` redundant (make sure to change the parameters of the `println!`
|
||||||
first `println!`"""
|
statements if you go this route)
|
||||||
|
"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "move_semantics3"
|
name = "move_semantics3"
|
||||||
|
|
Loading…
Reference in a new issue