Merge pull request #1660 from rust-lang/fix/move-semantics-tests
fix: refactor move semantics 1-4 into tests
This commit is contained in:
commit
abc3013096
|
@ -5,24 +5,19 @@
|
|||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let vec0 = Vec::new();
|
||||
let vec0 = vec![22, 44, 66];
|
||||
|
||||
let vec1 = fill_vec(vec0);
|
||||
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
|
||||
vec1.push(88);
|
||||
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||
}
|
||||
|
||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
let mut vec = vec;
|
||||
let vec = vec;
|
||||
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
vec.push(88);
|
||||
|
||||
vec
|
||||
}
|
||||
|
|
|
@ -1,32 +1,26 @@
|
|||
// move_semantics2.rs
|
||||
//
|
||||
// Expected output:
|
||||
// vec0 has length 3, with contents `[22, 44, 66]`
|
||||
// vec1 has length 4, with contents `[22, 44, 66, 88]`
|
||||
// Make the test pass by finding a way to keep both Vecs separate!
|
||||
//
|
||||
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
|
||||
// for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let vec0 = Vec::new();
|
||||
let vec0 = vec![22, 44, 66];
|
||||
|
||||
let mut vec1 = fill_vec(vec0);
|
||||
|
||||
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
|
||||
|
||||
vec1.push(88);
|
||||
|
||||
println!("{} has length {}, with contents `{:?}`", "vec1", vec1.len(), vec1);
|
||||
assert_eq!(vec0, vec![22, 44, 66]);
|
||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||
}
|
||||
|
||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
let mut vec = vec;
|
||||
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
vec.push(88);
|
||||
|
||||
vec
|
||||
}
|
||||
|
|
|
@ -8,22 +8,17 @@
|
|||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let vec0 = Vec::new();
|
||||
let vec0 = vec![22, 44, 66];
|
||||
|
||||
let mut vec1 = fill_vec(vec0);
|
||||
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
|
||||
vec1.push(88);
|
||||
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||
}
|
||||
|
||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
vec.push(88);
|
||||
|
||||
vec
|
||||
}
|
||||
|
|
|
@ -9,25 +9,21 @@
|
|||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let vec0 = Vec::new();
|
||||
let vec0 = vec![22, 44, 66];
|
||||
|
||||
let mut vec1 = fill_vec(vec0);
|
||||
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
|
||||
vec1.push(88);
|
||||
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||
}
|
||||
|
||||
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
|
||||
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
|
||||
fn fill_vec() -> Vec<i32> {
|
||||
// Instead, let's create and fill the Vec in here - how do you do that?
|
||||
let mut vec = vec;
|
||||
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
vec.push(88);
|
||||
|
||||
vec
|
||||
}
|
||||
|
|
16
info.toml
16
info.toml
|
@ -284,9 +284,9 @@ better. What do you think is the more commonly used pattern under Rust developer
|
|||
[[exercises]]
|
||||
name = "move_semantics1"
|
||||
path = "exercises/move_semantics/move_semantics1.rs"
|
||||
mode = "compile"
|
||||
mode = "test"
|
||||
hint = """
|
||||
So you've got the "cannot borrow immutable local variable `vec1` as mutable" error on the line
|
||||
So you've got the "cannot borrow immutable local variable `vec` as mutable" error on the line
|
||||
where we push an element to the vector, right?
|
||||
The fix for this is going to be adding one keyword, and the addition is NOT on the line where
|
||||
we push to the vector (where the error is).
|
||||
|
@ -296,7 +296,7 @@ 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 = """
|
||||
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
|
||||
|
@ -309,16 +309,12 @@ Rust provides a couple of different ways to mitigate this issue, feel free to tr
|
|||
2. Make `fill_vec` borrow its argument instead of taking ownership of it,
|
||||
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"
|
||||
path = "exercises/move_semantics/move_semantics3.rs"
|
||||
mode = "compile"
|
||||
mode = "test"
|
||||
hint = """
|
||||
The difference between this one and the previous ones is that the first line
|
||||
of `fn fill_vec` that had `let mut vec = vec;` is no longer there. You can,
|
||||
|
@ -328,7 +324,7 @@ an existing binding to be a mutable binding instead of an immutable one :)"""
|
|||
[[exercises]]
|
||||
name = "move_semantics4"
|
||||
path = "exercises/move_semantics/move_semantics4.rs"
|
||||
mode = "compile"
|
||||
mode = "test"
|
||||
hint = """
|
||||
Stop reading whenever you feel like you have enough direction :) Or try
|
||||
doing one step and then fixing the compiler errors that result!
|
||||
|
@ -337,7 +333,7 @@ So the end goal is to:
|
|||
- so then `vec0` doesn't exist, so we can't pass it to `fill_vec`
|
||||
- `fill_vec` has had its signature changed, which our call should reflect
|
||||
- since we're not creating a new vec in `main` anymore, we need to create
|
||||
a new vec in `fill_vec`, similarly to the way we did in `main`"""
|
||||
a new vec in `fill_vec`, and fill it with the expected values"""
|
||||
|
||||
[[exercises]]
|
||||
name = "move_semantics5"
|
||||
|
|
Loading…
Reference in a new issue