diff options
| author | liv <shadows_withal@fastmail.com> | 2023-06-12 12:07:18 +0200 |
|---|---|---|
| committer | liv <shadows_withal@fastmail.com> | 2023-06-12 12:07:18 +0200 |
| commit | 369ae2e63d06de6fee36aeebfd1ff3e8bcdfa25a (patch) | |
| tree | 0cfe23c0558eb30dc6dc3bffa17649f4e33e3c8c /info.toml | |
| parent | 1ce9d93e94231d20d3cc0b4d4c4498a0a077e494 (diff) | |
feat(move_semantics2): rewrite hint
Diffstat (limited to 'info.toml')
| -rw-r--r-- | info.toml | 29 |
1 files changed, 15 insertions, 14 deletions
@@ -287,23 +287,24 @@ Also: Try accessing `vec0` after having called `fill_vec()`. See what happens!"" [[exercises]] name = "move_semantics2" path = "exercises/move_semantics/move_semantics2.rs" -mode = "compile" +mode = "test" hint = """ -So, `vec0` is passed into the `fill_vec` function as an argument. In Rust, -when an argument is passed to a function and it's not explicitly returned, -you can't use the original variable anymore. We call this "moving" a variable. -Variables that are moved into a function (or block scope) and aren't explicitly -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 +When running this exercise for the first time, you'll notice an error about +"borrow of moved value". In Rust, when an argument is passed to a function and +it's not explicitly returned, you can't use the original variable anymore. +We call this "moving" a variable. When we pass `vec0` into `fill_vec`, it's being +"moved" into `vec1`, meaning we can't access `vec0` anymore after the fact. +Rust provides a couple of different ways to mitigate this issue, feel free to try them all: +1. You could make another, separate version of the data that's in `vec0` and pass that to `fill_vec` instead. 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 - `Vec<i32>` -3. 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 - of `vec1` entirely -- note that this will change what gets printed by the - first `println!`""" + and then copy the data within the function (`vec.clone()`) in order to return an owned + `Vec<i32>`. +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. This means that `vec0` will change over the + course of the function, and makes `vec1` redundant (make sure to change the parameters of the `println!` + statements if you go this route) +""" [[exercises]] name = "move_semantics3" |
