mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 23:04:17 -05:00
fix: refactor move semantics 1-4 into tests
This commit is contained in:
parent
c177507db3
commit
51e237d5f9
5 changed files with 27 additions and 51 deletions
|
@ -5,24 +5,19 @@
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
let vec1 = fill_vec(vec0);
|
let vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||||
|
|
||||||
vec1.push(88);
|
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
let mut vec = vec;
|
let vec = vec;
|
||||||
|
|
||||||
vec.push(22);
|
vec.push(88);
|
||||||
vec.push(44);
|
|
||||||
vec.push(66);
|
|
||||||
|
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +1,26 @@
|
||||||
// move_semantics2.rs
|
// move_semantics2.rs
|
||||||
//
|
//
|
||||||
// Expected output:
|
// Make the test pass by finding a way to keep both Vecs separate!
|
||||||
// vec0 has length 3, with contents `[22, 44, 66]`
|
|
||||||
// vec1 has length 4, with contents `[22, 44, 66, 88]`
|
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
|
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
|
||||||
// for a hint.
|
// for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);
|
assert_eq!(vec0, vec![22, 44, 66]);
|
||||||
|
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||||
vec1.push(88);
|
|
||||||
|
|
||||||
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> {
|
||||||
let mut vec = vec;
|
let mut vec = vec;
|
||||||
|
|
||||||
vec.push(22);
|
vec.push(88);
|
||||||
vec.push(44);
|
|
||||||
vec.push(66);
|
|
||||||
|
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,22 +8,17 @@
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||||
|
|
||||||
vec1.push(88);
|
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
vec.push(22);
|
vec.push(88);
|
||||||
vec.push(44);
|
|
||||||
vec.push(66);
|
|
||||||
|
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,25 +9,21 @@
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||||
|
|
||||||
vec1.push(88);
|
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// `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> {
|
fn fill_vec() -> Vec<i32> {
|
||||||
|
// Instead, let's create and fill the Vec in here - how do you do that?
|
||||||
let mut vec = vec;
|
let mut vec = vec;
|
||||||
|
|
||||||
vec.push(22);
|
vec.push(88);
|
||||||
vec.push(44);
|
|
||||||
vec.push(66);
|
|
||||||
|
|
||||||
vec
|
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]]
|
[[exercises]]
|
||||||
name = "move_semantics1"
|
name = "move_semantics1"
|
||||||
path = "exercises/move_semantics/move_semantics1.rs"
|
path = "exercises/move_semantics/move_semantics1.rs"
|
||||||
mode = "compile"
|
mode = "test"
|
||||||
hint = """
|
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?
|
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
|
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).
|
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]]
|
[[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 = """
|
||||||
When running this exercise for the first time, you'll notice an error about
|
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
|
"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,
|
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
|
and then copy the data within the function (`vec.clone()`) in order to return an owned
|
||||||
`Vec<i32>`.
|
`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]]
|
[[exercises]]
|
||||||
name = "move_semantics3"
|
name = "move_semantics3"
|
||||||
path = "exercises/move_semantics/move_semantics3.rs"
|
path = "exercises/move_semantics/move_semantics3.rs"
|
||||||
mode = "compile"
|
mode = "test"
|
||||||
hint = """
|
hint = """
|
||||||
The difference between this one and the previous ones is that the first line
|
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,
|
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]]
|
[[exercises]]
|
||||||
name = "move_semantics4"
|
name = "move_semantics4"
|
||||||
path = "exercises/move_semantics/move_semantics4.rs"
|
path = "exercises/move_semantics/move_semantics4.rs"
|
||||||
mode = "compile"
|
mode = "test"
|
||||||
hint = """
|
hint = """
|
||||||
Stop reading whenever you feel like you have enough direction :) Or try
|
Stop reading whenever you feel like you have enough direction :) Or try
|
||||||
doing one step and then fixing the compiler errors that result!
|
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`
|
- 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
|
- `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
|
- 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]]
|
[[exercises]]
|
||||||
name = "move_semantics5"
|
name = "move_semantics5"
|
||||||
|
|
Loading…
Reference in a new issue