From fe724b043715a2d3be2a65c84e5a1f32b63795b9 Mon Sep 17 00:00:00 2001 From: Suzie Kim Date: Wed, 31 Jan 2024 15:59:06 -0500 Subject: [PATCH] Complete move semantics exercises --- .../06_move_semantics/move_semantics1.rs | 4 +-- .../06_move_semantics/move_semantics2.rs | 15 ++++++--- .../06_move_semantics/move_semantics3.rs | 6 ++-- .../06_move_semantics/move_semantics4.rs | 10 +++--- .../06_move_semantics/move_semantics5.rs | 32 ++++++++++++++----- .../06_move_semantics/move_semantics6.rs | 28 ++++++++++++---- 6 files changed, 64 insertions(+), 31 deletions(-) diff --git a/exercises/06_move_semantics/move_semantics1.rs b/exercises/06_move_semantics/move_semantics1.rs index e0639375..3b76ccc6 100644 --- a/exercises/06_move_semantics/move_semantics1.rs +++ b/exercises/06_move_semantics/move_semantics1.rs @@ -3,8 +3,6 @@ // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE - #[test] fn main() { let vec0 = vec![22, 44, 66]; @@ -15,7 +13,7 @@ fn main() { } fn fill_vec(vec: Vec) -> Vec { - let vec = vec; + let mut vec = vec; vec.push(88); diff --git a/exercises/06_move_semantics/move_semantics2.rs b/exercises/06_move_semantics/move_semantics2.rs index baf6bcc9..0bdccdfd 100644 --- a/exercises/06_move_semantics/move_semantics2.rs +++ b/exercises/06_move_semantics/move_semantics2.rs @@ -5,16 +5,23 @@ // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE - #[test] fn main() { let vec0 = vec![22, 44, 66]; - let mut vec1 = fill_vec(vec0); + let vec1 = fill_vec(vec0.clone()); + + // When vec0 is passed to fill_vec, the function takes OWNERSHIP + // of the vector. It mutates the vector and passes ownershpip to vec1. + // Once a value has moved, we can no longer use it. + // this is why, we need to clone it so that we can use it again + + // Using clone() makes an entire copy of the vector. DEEP COPY. + // It is also a separate object in memory. + // Changes to the original vector does not affect the cloned vector and vice versa assert_eq!(vec0, vec![22, 44, 66]); - assert_eq!(vec1, vec![22, 44, 66, 88]); + assert_eq!(vec1, vec![22, 44, 66, 88]); } fn fill_vec(vec: Vec) -> Vec { diff --git a/exercises/06_move_semantics/move_semantics3.rs b/exercises/06_move_semantics/move_semantics3.rs index 7af9e694..4cecff92 100644 --- a/exercises/06_move_semantics/move_semantics3.rs +++ b/exercises/06_move_semantics/move_semantics3.rs @@ -6,8 +6,6 @@ // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE - #[test] fn main() { let vec0 = vec![22, 44, 66]; @@ -17,8 +15,8 @@ fn main() { assert_eq!(vec1, vec![22, 44, 66, 88]); } -fn fill_vec(vec: Vec) -> Vec { +fn fill_vec(mut vec: Vec) -> Vec { vec.push(88); vec -} +} \ No newline at end of file diff --git a/exercises/06_move_semantics/move_semantics4.rs b/exercises/06_move_semantics/move_semantics4.rs index 80b49dba..1a258ab2 100644 --- a/exercises/06_move_semantics/move_semantics4.rs +++ b/exercises/06_move_semantics/move_semantics4.rs @@ -7,13 +7,11 @@ // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE - #[test] fn main() { let vec0 = vec![22, 44, 66]; - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(); assert_eq!(vec1, vec![22, 44, 66, 88]); } @@ -21,8 +19,10 @@ fn main() { // `fill_vec()` no longer takes `vec: Vec` as argument - don't change this! fn fill_vec() -> Vec { // Instead, let's create and fill the Vec in here - how do you do that? - let mut vec = vec; - + let mut vec: Vec = Vec::new(); + vec.push(22); + vec.push(44); + vec.push(66); vec.push(88); vec diff --git a/exercises/06_move_semantics/move_semantics5.rs b/exercises/06_move_semantics/move_semantics5.rs index 267bdccc..6f8fe22f 100644 --- a/exercises/06_move_semantics/move_semantics5.rs +++ b/exercises/06_move_semantics/move_semantics5.rs @@ -6,14 +6,30 @@ // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE - #[test] +// Adding the curly braces makes the the scope explicit. +// This is just more readable and clear to people to reading. fn main() { let mut x = 100; - let y = &mut x; - let z = &mut x; - *y += 100; - *z += 1000; - assert_eq!(x, 1200); -} + { + let y = &mut x; + *y += 100; + } + { + let z = &mut x; + *z += 1000; + } + assert_eq!(x, 1200); + } + +// fn main() { + // let mut x = 100; + + // scopes x and here and then y is out of scope afterwards + //let y = &mut x; + //*y += 100; + + //let z = &mut x; + //*z += 1000; + //assert_eq!(x, 1200); +//} diff --git a/exercises/06_move_semantics/move_semantics6.rs b/exercises/06_move_semantics/move_semantics6.rs index cace4ca6..349191fa 100644 --- a/exercises/06_move_semantics/move_semantics6.rs +++ b/exercises/06_move_semantics/move_semantics6.rs @@ -5,24 +5,38 @@ // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand // for a hint. -// I AM NOT DONE - fn main() { let data = "Rust is great!".to_string(); - get_char(data); + // get_char is taking ownership of the data + // a reference is like a pointer. + // it's an address to the data that we want to access. + // the difference though is that a reference is guaranteed to point to a valid value + // of a particular type for the life of that + get_char(&data); - string_uppercase(&data); + string_uppercase(data); } // Should not take ownership -fn get_char(data: String) -> char { + +//In this case, the function is taking a reference to avoid data copying +// The original string is needed after the function call. +// get_char does not modify the data, only reads it so we can just pass the reference. + + +fn get_char(data: &String) -> char { data.chars().last().unwrap() + // This function is getting the last character of the string reference + // in this test case, i would be '!' + // println!('{}'. char); would print '!' } // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +// because it is modifying the value of data +// the mut in the parameters allows it to reassign a value to the 'data' +fn string_uppercase(mut data: String) { + data = data.to_uppercase(); println!("{}", data); }