feat(move_semantics): clarify some hints

This commit is contained in:
mokou 2022-07-12 15:25:31 +02:00
parent 7452d0d603
commit bb0cf92b8b
3 changed files with 11 additions and 8 deletions

View file

@ -1,5 +1,5 @@
// move_semantics1.rs // move_semantics1.rs
// Make me compile! Execute `rustlings hint move_semantics1` for hints :) // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM NOT DONE

View file

@ -1,6 +1,6 @@
// move_semantics2.rs // move_semantics2.rs
// Make me compile without changing line 13 or moving line 10! // Make me compile without changing line 13 or moving line 10!
// Execute `rustlings hint move_semantics2` for hints :) // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM NOT DONE

View file

@ -280,18 +280,21 @@ mode = "compile"
hint = """ hint = """
So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on line 13, So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on line 13,
right? The fix for this is going to be adding one keyword, and the addition is NOT on line 13 right? The fix for this is going to be adding one keyword, and the addition is NOT on line 13
where the error is.""" where the error is.
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 = "compile"
hint = """ hint = """
So `vec0` is being *moved* into the function `fill_vec` when we call it on So, `vec0` is passed into the `fill_vec` function as an argument. In Rust,
line 10, which means it gets dropped at the end of `fill_vec`, which means we when an argument is passed to a function and it's not explicitly returned,
can't use `vec0` again on line 13 (or anywhere else in `main` after the you can't use the original variable anymore. We call this "moving" a variable.
`fill_vec` call for that matter). We could fix this in a few ways, try them Variables that are moved into a function (or block scope) and aren't explicitly
all! returned get "dropped" at the end of that function. This is also what happens here.
There's a few ways to fix this, try them all if you want:
1. Make another, separate version of the data that's in `vec0` and pass that 1. 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,