From 867bf72e8a144fb66f8eecc1f1cd948bf874fa64 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Fri, 18 Sep 2026 16:41:38 -0500 Subject: rustlings: complete 06 --- .../exercises/06_move_semantics/move_semantics1.rs | 3 +-- .../exercises/06_move_semantics/move_semantics2.rs | 2 +- .../exercises/06_move_semantics/move_semantics3.rs | 2 +- .../exercises/06_move_semantics/move_semantics4.rs | 2 +- .../exercises/06_move_semantics/move_semantics5.rs | 8 +++---- .../solutions/06_move_semantics/move_semantics1.rs | 25 +++++++++++++++++-- .../solutions/06_move_semantics/move_semantics2.rs | 28 ++++++++++++++++++++-- .../solutions/06_move_semantics/move_semantics3.rs | 22 +++++++++++++++-- .../solutions/06_move_semantics/move_semantics4.rs | 19 +++++++++++++-- .../solutions/06_move_semantics/move_semantics5.rs | 23 ++++++++++++++++-- 10 files changed, 115 insertions(+), 19 deletions(-) diff --git a/rustlings/exercises/06_move_semantics/move_semantics1.rs b/rustlings/exercises/06_move_semantics/move_semantics1.rs index 4eb3d61..9385f28 100644 --- a/rustlings/exercises/06_move_semantics/move_semantics1.rs +++ b/rustlings/exercises/06_move_semantics/move_semantics1.rs @@ -1,6 +1,5 @@ -// TODO: Fix the compiler error in this function. fn fill_vec(vec: Vec) -> Vec { - let vec = vec; + let mut vec = vec; vec.push(88); diff --git a/rustlings/exercises/06_move_semantics/move_semantics2.rs b/rustlings/exercises/06_move_semantics/move_semantics2.rs index a3ab7a0..e205b81 100644 --- a/rustlings/exercises/06_move_semantics/move_semantics2.rs +++ b/rustlings/exercises/06_move_semantics/move_semantics2.rs @@ -20,7 +20,7 @@ mod tests { fn move_semantics2() { let vec0 = vec![22, 44, 66]; - let vec1 = fill_vec(vec0); + let vec1 = fill_vec(vec0.clone()); assert_eq!(vec0, [22, 44, 66]); assert_eq!(vec1, [22, 44, 66, 88]); diff --git a/rustlings/exercises/06_move_semantics/move_semantics3.rs b/rustlings/exercises/06_move_semantics/move_semantics3.rs index 11dbbbe..4a90c21 100644 --- a/rustlings/exercises/06_move_semantics/move_semantics3.rs +++ b/rustlings/exercises/06_move_semantics/move_semantics3.rs @@ -1,5 +1,5 @@ // TODO: Fix the compiler error in the function without adding any new line. -fn fill_vec(vec: Vec) -> Vec { +fn fill_vec(mut vec: Vec) -> Vec { vec.push(88); vec diff --git a/rustlings/exercises/06_move_semantics/move_semantics4.rs b/rustlings/exercises/06_move_semantics/move_semantics4.rs index 56da988..89b412f 100644 --- a/rustlings/exercises/06_move_semantics/move_semantics4.rs +++ b/rustlings/exercises/06_move_semantics/move_semantics4.rs @@ -10,8 +10,8 @@ mod tests { fn move_semantics4() { let mut x = Vec::new(); let y = &mut x; - let z = &mut x; y.push(42); + let z = &mut x; z.push(13); assert_eq!(x, [42, 13]); } diff --git a/rustlings/exercises/06_move_semantics/move_semantics5.rs b/rustlings/exercises/06_move_semantics/move_semantics5.rs index cd0dafd..78d8c77 100644 --- a/rustlings/exercises/06_move_semantics/move_semantics5.rs +++ b/rustlings/exercises/06_move_semantics/move_semantics5.rs @@ -4,12 +4,12 @@ // removing references (the character `&`). // Shouldn't take ownership -fn get_char(data: String) -> char { +fn get_char(data: &String) -> char { data.chars().last().unwrap() } // Should take ownership -fn string_uppercase(mut data: &String) { +fn string_uppercase(mut data: String) { data = data.to_uppercase(); println!("{data}"); @@ -18,7 +18,7 @@ fn string_uppercase(mut data: &String) { fn main() { let data = "Rust is great!".to_string(); - get_char(data); + get_char(&data); - string_uppercase(&data); + string_uppercase(data); } diff --git a/rustlings/solutions/06_move_semantics/move_semantics1.rs b/rustlings/solutions/06_move_semantics/move_semantics1.rs index dcf2377..ac34e7a 100644 --- a/rustlings/solutions/06_move_semantics/move_semantics1.rs +++ b/rustlings/solutions/06_move_semantics/move_semantics1.rs @@ -1,4 +1,25 @@ +fn fill_vec(vec: Vec) -> Vec { + let mut vec = vec; + // ^^^ added + + vec.push(88); + + vec +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn move_semantics1() { + let vec0 = vec![22, 44, 66]; + let vec1 = fill_vec(vec0); + // `vec0` can't be accessed anymore because it is moved to `fill_vec`. + assert_eq!(vec1, vec![22, 44, 66, 88]); + } } diff --git a/rustlings/solutions/06_move_semantics/move_semantics2.rs b/rustlings/solutions/06_move_semantics/move_semantics2.rs index dcf2377..7bcd33a 100644 --- a/rustlings/solutions/06_move_semantics/move_semantics2.rs +++ b/rustlings/solutions/06_move_semantics/move_semantics2.rs @@ -1,4 +1,28 @@ +fn fill_vec(vec: Vec) -> Vec { + let mut vec = vec; + + vec.push(88); + + vec +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn move_semantics2() { + let vec0 = vec![22, 44, 66]; + + // Cloning `vec0` so that the clone is moved into `fill_vec`, not `vec0` + // itself. + let vec1 = fill_vec(vec0.clone()); + + assert_eq!(vec0, [22, 44, 66]); + assert_eq!(vec1, [22, 44, 66, 88]); + } } diff --git a/rustlings/solutions/06_move_semantics/move_semantics3.rs b/rustlings/solutions/06_move_semantics/move_semantics3.rs index dcf2377..7ba4006 100644 --- a/rustlings/solutions/06_move_semantics/move_semantics3.rs +++ b/rustlings/solutions/06_move_semantics/move_semantics3.rs @@ -1,4 +1,22 @@ +fn fill_vec(mut vec: Vec) -> Vec { + // ^^^ added + vec.push(88); + + vec +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn move_semantics3() { + let vec0 = vec![22, 44, 66]; + let vec1 = fill_vec(vec0); + assert_eq!(vec1, [22, 44, 66, 88]); + } } diff --git a/rustlings/solutions/06_move_semantics/move_semantics4.rs b/rustlings/solutions/06_move_semantics/move_semantics4.rs index dcf2377..1a39d4f 100644 --- a/rustlings/solutions/06_move_semantics/move_semantics4.rs +++ b/rustlings/solutions/06_move_semantics/move_semantics4.rs @@ -1,4 +1,19 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + #[test] + fn move_semantics4() { + let mut x = Vec::new(); + let y = &mut x; + // `y` used here. + y.push(42); + // The mutable reference `y` is not used anymore, + // therefore a new reference can be created. + let z = &mut x; + z.push(13); + assert_eq!(x, [42, 13]); + } } diff --git a/rustlings/solutions/06_move_semantics/move_semantics5.rs b/rustlings/solutions/06_move_semantics/move_semantics5.rs index dcf2377..1410e91 100644 --- a/rustlings/solutions/06_move_semantics/move_semantics5.rs +++ b/rustlings/solutions/06_move_semantics/move_semantics5.rs @@ -1,4 +1,23 @@ +#![allow(clippy::ptr_arg)] + +// Borrows instead of taking ownership. +// It is recommended to use `&str` instead of `&String` here. But this is +// enough for now because we didn't handle strings yet. +fn get_char(data: &String) -> char { + data.chars().last().unwrap() +} + +// Takes ownership instead of borrowing. +fn string_uppercase(mut data: String) { + data = data.to_uppercase(); + + println!("{data}"); +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let data = "Rust is great!".to_string(); + + get_char(&data); + + string_uppercase(data); } -- cgit v1.2.3