Solve move_semantics

This commit is contained in:
Manuel Gil 2020-03-05 09:31:21 +00:00
parent cd10ddb372
commit 7e1906a2a1
4 changed files with 9 additions and 19 deletions

View File

@ -1,12 +1,10 @@
// move_semantics1.rs // move_semantics1.rs
// Make me compile! Execute `rustlings hint move_semantics1` for hints :) // Make me compile! Execute `rustlings hint move_semantics1` for hints :)
// I AM NOT DONE
fn main() { fn main() {
let vec0 = Vec::new(); let vec0 = Vec::new();
let vec1 = fill_vec(vec0); let mut vec1 = fill_vec(vec0);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

View File

@ -5,24 +5,20 @@
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let vec0 = Vec::new(); let mut vec0 = Vec::new();
let mut vec1 = fill_vec(vec0); fill_vec(&mut vec0);
// Do not change the following line! // Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec1.push(88); vec0.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
} }
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(vec: &mut Vec<i32>) {
let mut vec = vec;
vec.push(22); vec.push(22);
vec.push(44); vec.push(44);
vec.push(66); vec.push(66);
vec
} }

View File

@ -17,7 +17,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
} }
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(22); vec.push(22);
vec.push(44); vec.push(44);
vec.push(66); vec.push(66);

View File

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