add solutions to exercises move_semantics

This commit is contained in:
Eri Mendz 2021-05-25 14:47:39 +03:00
parent 118e204236
commit 1d186e5644
3 changed files with 20 additions and 9 deletions

View file

@ -2,18 +2,23 @@
// Make me compile without changing line 13!
// Execute `rustlings hint move_semantics2` for hints :)
// I AM NOT DONE
// done may 25
fn main() {
// make empty Vec
let vec0 = Vec::new();
let mut vec1 = fill_vec(vec0);
// make vec1 as clone of vec0 and run the function fill_vec
let mut vec1 = fill_vec(vec0.clone());
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec1.push(88);
// before push of last element
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
// after push of last element
vec1.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}

View file

@ -3,11 +3,12 @@
// (no lines with multiple semicolons necessary!)
// Execute `rustlings hint move_semantics3` for hints :)
// I AM NOT DONE
// DONE May 25/21
fn main() {
let vec0 = Vec::new();
// make call to function fill_vec
let mut vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -15,9 +16,12 @@ fn main() {
vec1.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
vec1.pop(); //remove last element of vec
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
// add mut keyword to vec
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);

View file

@ -4,12 +4,14 @@
// freshly created vector from fill_vec to its caller.
// Execute `rustlings hint move_semantics4` for hints!
// I AM NOT DONE
// may 25/21 DONE
fn main() {
let vec0 = Vec::new();
// disabled the below line and mv inside the function, see above comments
//let vec0 = Vec::new();
let mut vec1 = fill_vec(vec0);
//let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec();
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
@ -20,7 +22,7 @@ fn main() {
// `fill_vec()` no longer take `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);