From 26e5d491daa2fc0e321cc56d2461b12119c4b3fd Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 16:04:27 -0400 Subject: [PATCH] day2/move_semantics2 done --- exercises/move_semantics/move_semantics2.rs | 199 ++++++++++++++++-- .../move_semantics/move_semantics2.rs.bak | 67 ++++++ exercises/move_semantics/move_semantics3.rs | 29 ++- 3 files changed, 278 insertions(+), 17 deletions(-) create mode 100644 exercises/move_semantics/move_semantics2.rs.bak diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index 3704c198..2f2379da 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -2,30 +2,199 @@ // Make me compile without changing line 13 or moving line 10! // Execute `rustlings hint move_semantics2` for hints :) -// I AM NOT DONE +// // original code +// fn main() { +// // original code +// // needed for solution0/solution1 +// // comment out for solution2 +// let vec0 = Vec::new(); +// // solution3 +// // comment out for orignal/solution0/solution1/solution2 +// // create copy of vec0 +// let vec0_copy = vec0.clone(); + +// // solution2 (part0) +// // comment out for orignal/solution0/solution1 +// // let mut vec0 = &mut Vec::new(); + +// // original code +// // comment out for solution0/solution1/solution2/solution3 +// // let mut vec1 = fill_vec(vec0); + +// // solution0 (part0) +// // comment out for orignal/solution1/solution2 +// // let mut vec0_ref = &vec0; + +// // solution0 (part1) +// // comment out for orignal/solution1/solution2 +// // let mut vec1 = fill_vec(vec0_ref.to_vec()); + +// // solution1 (part0) +// // comment out for orignal/solution0/solution2 +// // let mut vec1 = fill_vec(&vec0); + +// // solution2 (part1) +// // comment out for original/solution0/solution1 +// // fill_vec(vec0); + +// // solution3 (part1) +// // pass vec0_copy to fill_vec +// let mut vec1 = fill_vec(vec0_copy); + +// // original code +// // Do not change the following line! +// println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); + +// // solution2 (part2) +// // comment out for original/solution0/solution1 +// // let mut vec1 = &mut Vec::new(); +// // fill_vec(vec1); + +// // original code +// // needed for original and all solutions +// vec1.push(88); + +// // original code +// // needed for original and all solutions +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +// } + +// // original code +// // needed for original and solution0 +// // comment out for all solution1/solution2 +// fn fill_vec(vec: Vec) -> Vec { +// let mut vec = vec; + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } + +// solution1 (part1) +// comment out for original/solution0/solution2 +// fn fill_vec(vec: &Vec) -> Vec { +// let mut vec = vec.to_vec(); + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } + +// solution2 (part0) +// comment out for original/solution0/solution1 +// fn fill_vec(vec: &mut Vec) { +// let mut vec = vec; + +// vec.push(22); +// vec.push(44); +// vec.push(66); +// } + +// // original code +// fn main() { +// let vec0 = Vec::new(); + +// let mut vec1 = fill_vec(vec0); + +// // Do not change the following line! +// println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); + +// vec1.push(88); + +// // Do not change the following line! +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +// } + +// // original code (cont'd) +// fn fill_vec(vec: Vec) -> Vec { +// let mut vec = vec; + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } +// // end of original code + +// solution0 +// fn main() { +// let vec0 = Vec::new(); + +// // create copy of vec0 +// let vec0_copy = vec0.clone(); + +// // pass vec0_copy to fill_vec +// let mut vec1 = fill_vec(vec0_copy); + +// // Do not change the following line! +// println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); +// vec1.push(88); + +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +// } + +// // solution0 (cont'd) +// fn fill_vec(vec: Vec) -> Vec { +// let mut vec = vec; + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } +// // end of solution0 + +// // solution1 +// fn main() { +// let vec0 = Vec::new(); + +// let mut vec1 = fill_vec(&vec0); + +// // Do not change the following line! +// println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); + +// vec1.push(88); + +// // Do not change the following line! +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +// } + +// // solution1 (cont'd) +// fn fill_vec(vec: &Vec) -> Vec { +// // convert borrowed vec to mutable copy +// let mut vec = vec.to_vec().clone(); + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } +// // end of solution1 + +// solution2 fn main() { - let vec0 = Vec::new(); + // create mutable ref to vec + let mut vec0 = &mut Vec::new(); - // solution 1 - // let mut vec0_copy = &vec0; - // let mut vec1 = fill_vec(vec0_copy.to_vec()); - let mut vec1 = fill_vec(vec0); + fill_vec(vec0); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); - - vec1.push(88); - - println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { - let mut vec = vec; - +// solution2 (cont'd) +// accept mutable ref to vec as argument and act on it directly +fn fill_vec(vec: &mut Vec) { vec.push(22); vec.push(44); vec.push(66); - - vec } +// end of solution2 diff --git a/exercises/move_semantics/move_semantics2.rs.bak b/exercises/move_semantics/move_semantics2.rs.bak new file mode 100644 index 00000000..86a4ec62 --- /dev/null +++ b/exercises/move_semantics/move_semantics2.rs.bak @@ -0,0 +1,67 @@ +// move_semantics2.rs +// Make me compile without changing line 13 or moving line 10! +// Execute `rustlings hint move_semantics2` for hints :) + +// I AM NOT DONE + +fn main() { + // original code + // comment out for solution1/solution2 + let vec0 = Vec::new(); + + // solution1 (part0) + // let mut vec0 = Vec::new(); + + // solution2 (part0) + // let vec0 = &mut Vec::new(); + + // solution0 + let mut vec0_copy = &vec0; + let mut vec1 = fill_vec(vec0_copy.to_vec()); + + // original code + let mut vec1 = fill_vec(vec0); + + // solution1 (part0) + // let mut vec1 = fill_vec(&vec0); + + // Do not change the following line! + println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); + + vec1.push(88); + + println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +} + +// original code +fn fill_vec(vec: Vec) -> Vec { + let mut vec = vec; + + vec.push(22); + vec.push(44); + vec.push(66); + + vec +} + +// solution1 (part1) +// fn fill_vec(vec: &Vec) -> Vec { +// let mut vec = vec.to_vec(); + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } + +// solution2 +// fn fill_vec(vec: &mut Vec) -> Vec { +// let mut vec = vec.clone(); + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index 43fef74f..2706cf7e 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -3,8 +3,30 @@ // (no lines with multiple semicolons necessary!) // Execute `rustlings hint move_semantics3` for hints :) -// I AM NOT DONE +// // original code +// fn main() { +// let vec0 = Vec::new(); +// let mut vec1 = fill_vec(vec0); + +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); + +// vec1.push(88); + +// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); +// } + +// // original code (cont'd) +// fn fill_vec(vec: Vec) -> Vec { +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } +// // end of original code + +// solution fn main() { let vec0 = Vec::new(); @@ -17,10 +39,13 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { +// solution (cont'd) +// change vec argument to mutable +fn fill_vec(mut vec: Vec) -> Vec { vec.push(22); vec.push(44); vec.push(66); vec } +// end of solution