From bb0cf92b8b6f0a9db4be92ff5ffc4c507f2847ba Mon Sep 17 00:00:00 2001 From: mokou Date: Tue, 12 Jul 2022 15:25:31 +0200 Subject: [PATCH] feat(move_semantics): clarify some hints --- exercises/move_semantics/move_semantics1.rs | 2 +- exercises/move_semantics/move_semantics2.rs | 2 +- info.toml | 15 +++++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index e2f5876..aac6dfc 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,5 +1,5 @@ // 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 diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index 888dc52..6487085 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -1,6 +1,6 @@ // move_semantics2.rs // 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 diff --git a/info.toml b/info.toml index c623120..a7c46eb 100644 --- a/info.toml +++ b/info.toml @@ -280,18 +280,21 @@ mode = "compile" hint = """ 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 -where the error is.""" +where the error is. + +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" hint = """ -So `vec0` is being *moved* into the function `fill_vec` when we call it on -line 10, which means it gets dropped at the end of `fill_vec`, which means we -can't use `vec0` again on line 13 (or anywhere else in `main` after the -`fill_vec` call for that matter). We could fix this in a few ways, try them -all! +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 to `fill_vec` instead. 2. Make `fill_vec` borrow its argument instead of taking ownership of it,