Solved a few move exercises (#3)

This commit is contained in:
Jonathan Zernik 2022-06-04 22:20:55 -07:00 committed by GitHub
parent 7a6b39125e
commit 84a6c64b2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 12 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

@ -2,27 +2,49 @@
// Make me compile without changing line 13 or moving line 10! // Make me compile without changing line 13 or moving line 10!
// Execute `rustlings hint move_semantics2` for hints :) // Execute `rustlings hint move_semantics2` for hints :)
// I AM NOT DONE
fn main() { fn main() {
let vec0 = Vec::new(); //let vec0 = Vec::new();
let mut vec0 = Vec::new();
let mut vec1 = fill_vec(vec0); //let mut vec1 = fill_vec(vec0);
//let mut vec1 = fill_vec(vec0.clone());
//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); //vec1.push(88);
vec0.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); //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: Vec<i32>) -> Vec<i32> {
let mut vec = vec; // let mut vec = vec;
// vec.push(22);
// vec.push(44);
// vec.push(66);
// vec
// }
// fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
// let mut vec = vec.clone();
// vec.push(22);
// vec.push(44);
// vec.push(66);
// vec
// }
fn fill_vec(vec: &mut Vec<i32>) -> () {
vec.push(22); vec.push(22);
vec.push(44); vec.push(44);
vec.push(66); vec.push(66);
vec ()
} }