Solve move semantics exercises.

This commit is contained in:
Leonardo Freua 2021-09-14 10:17:39 -03:00
parent 233dc4713f
commit f254de8d13
5 changed files with 18 additions and 28 deletions

View File

@ -1,12 +1,11 @@
// move_semantics1.rs
// Make me compile! Execute `rustlings hint move_semantics1` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let mut vec0 = Vec::new();
let vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(&mut vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -15,9 +14,9 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
fn fill_vec(vec: &mut Vec<i32>) -> &mut Vec<i32> {
let vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);

View File

@ -2,12 +2,10 @@
// Make me compile without changing line 13!
// Execute `rustlings hint move_semantics2` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let mut vec0 = Vec::new();
let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(&mut vec0);
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
@ -17,12 +15,12 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
fn fill_vec(vec: &mut Vec<i32>) -> Vec<i32> {
let vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);
vec
vec.to_vec()
}

View File

@ -3,12 +3,11 @@
// (no lines with multiple semicolons necessary!)
// Execute `rustlings hint move_semantics3` for hints :)
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let mut vec0 = Vec::new();
let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(&mut vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -17,7 +16,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn fill_vec(vec: &mut Vec<i32>) -> &mut Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);

View File

@ -4,12 +4,9 @@
// freshly created vector from fill_vec to its caller.
// Execute `rustlings hint move_semantics4` for hints!
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec();
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -18,9 +15,8 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
let mut vec = vec;
let mut vec = Vec::new();
vec.push(22);
vec.push(44);

View File

@ -1,15 +1,13 @@
// move_semantics5.rs
// Make me compile only be reordering the lines in `main()`, but without
// adding, changing or removing any of them.
// Make me compile without adding, removing, or changing any of the
// lines in `main()`.
// Execute `rustlings hint move_semantics5` for hints :)
// I AM NOT DONE
fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut *y;
*y += 100;
*z += 1000;
*y += 100;
assert_eq!(x, 1200);
}