From df3743c844a58ecdfee629c47c6e4596b73f3001 Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Mon, 16 May 2022 16:56:15 -0400 Subject: [PATCH] move_semantics done --- exercises/move_semantics/move_semantics4.rs | 36 ++++++++++++++--- exercises/move_semantics/move_semantics5.rs | 14 ++++++- exercises/move_semantics/move_semantics6.rs | 43 ++++++++++++++++++--- 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 2a23c710..c511fc61 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,35 @@ // freshly created vector from fill_vec to its caller. // Execute `rustlings hint move_semantics4` 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) +// // `fill_vec()` no longer takes `vec: Vec` as argument +// fn fill_vec() -> Vec { +// let mut vec = vec; + +// vec.push(22); +// vec.push(44); +// vec.push(66); + +// vec +// } +// // end of original code + +// solution 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 +41,11 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } +// solution (cont'd) // `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + // create mutable vec + let mut vec = Vec::new(); vec.push(22); vec.push(44); @@ -28,3 +53,4 @@ fn fill_vec() -> Vec { vec } +// end of solution diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index c4704f9e..956f7b6f 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -3,13 +3,23 @@ // adding, changing or removing any of them. // Execute `rustlings hint move_semantics5` for hints :) -// I AM NOT DONE +// // original code +// fn main() { +// let mut x = 100; +// let y = &mut x; +// let z = &mut x; +// *y += 100; +// *z += 1000; +// assert_eq!(x, 1200); +// } +// // end of orignal code +// solution fn main() { let mut x = 100; let y = &mut x; - let z = &mut x; *y += 100; + let z = &mut x; *z += 1000; assert_eq!(x, 1200); } diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index 457e7ae7..12ae9cad 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -2,24 +2,55 @@ // Make me compile! `rustlings hint move_semantics6` for hints // You can't change anything except adding or removing references -// I AM NOT DONE +// // original code +// fn main() { +// let data = "Rust is great!".to_string(); +// get_char(data); + +// string_uppercase(&data); +// } + +// // original code (cont'd) +// // Should not take ownership +// fn get_char(data: String) -> char { +// data.chars().last().unwrap() +// } + +// // original code (cont'd) +// // Should take ownership +// fn string_uppercase(mut data: &String) { +// data = &data.to_uppercase(); + +// println!("{}", data); +// } +// // end of original code + +// solution fn main() { let data = "Rust is great!".to_string(); - get_char(data); + // change passed data to ref so as to not take ownership of `data` + get_char(&data); - string_uppercase(&data); + // remove ref so string_uppercase takes ownership of `data` + string_uppercase(data); } +// solution (cont'd) // Should not take ownership -fn get_char(data: String) -> char { +// add ref to argument type +fn get_char(data: &String) -> char { data.chars().last().unwrap() } +// solution (cont'd) // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +// remove ref from argument type +fn string_uppercase(mut data: String) { + // remove ref from data + data = data.to_uppercase(); println!("{}", data); } +// end of solution