diff options
| author | mo8it <mo8it@proton.me> | 2024-07-05 13:39:50 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-07-05 13:39:50 +0200 |
| commit | 7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch) | |
| tree | c67f7e62bb9a179ae4fdbab492501cb6847e64c7 /exercises/06_move_semantics | |
| parent | 77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff) | |
| parent | 4d9c346a173bb722b929f3ea3c00f84954483e24 (diff) | |
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'exercises/06_move_semantics')
| -rw-r--r-- | exercises/06_move_semantics/move_semantics1.rs | 33 | ||||
| -rw-r--r-- | exercises/06_move_semantics/move_semantics2.rs | 38 | ||||
| -rw-r--r-- | exercises/06_move_semantics/move_semantics3.rs | 32 | ||||
| -rw-r--r-- | exercises/06_move_semantics/move_semantics4.rs | 39 | ||||
| -rw-r--r-- | exercises/06_move_semantics/move_semantics5.rs | 35 | ||||
| -rw-r--r-- | exercises/06_move_semantics/move_semantics6.rs | 28 |
6 files changed, 86 insertions, 119 deletions
diff --git a/exercises/06_move_semantics/move_semantics1.rs b/exercises/06_move_semantics/move_semantics1.rs index e063937..4eb3d61 100644 --- a/exercises/06_move_semantics/move_semantics1.rs +++ b/exercises/06_move_semantics/move_semantics1.rs @@ -1,19 +1,4 @@ -// move_semantics1.rs -// -// 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]; - - let vec1 = fill_vec(vec0); - - assert_eq!(vec1, vec![22, 44, 66, 88]); -} - +// TODO: Fix the compiler error in this function. fn fill_vec(vec: Vec<i32>) -> Vec<i32> { let vec = vec; @@ -21,3 +6,19 @@ fn fill_vec(vec: Vec<i32>) -> Vec<i32> { vec } + +fn main() { + // 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); + assert_eq!(vec1, vec![22, 44, 66, 88]); + } +} diff --git a/exercises/06_move_semantics/move_semantics2.rs b/exercises/06_move_semantics/move_semantics2.rs index baf6bcc..a3ab7a0 100644 --- a/exercises/06_move_semantics/move_semantics2.rs +++ b/exercises/06_move_semantics/move_semantics2.rs @@ -1,26 +1,28 @@ -// move_semantics2.rs -// -// Make the test pass by finding a way to keep both Vecs separate! -// -// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand -// for a hint. - -// I AM NOT DONE +fn fill_vec(vec: Vec<i32>) -> Vec<i32> { + let mut vec = vec; -#[test] -fn main() { - let vec0 = vec![22, 44, 66]; + vec.push(88); - let mut vec1 = fill_vec(vec0); + vec +} - assert_eq!(vec0, vec![22, 44, 66]); - assert_eq!(vec1, vec![22, 44, 66, 88]); +fn main() { + // You can optionally experiment here. } -fn fill_vec(vec: Vec<i32>) -> Vec<i32> { - let mut vec = vec; +#[cfg(test)] +mod tests { + use super::*; - vec.push(88); + // TODO: Make both vectors `vec0` and `vec1` accessible at the same time to + // fix the compiler error in the test. + #[test] + fn move_semantics2() { + let vec0 = vec![22, 44, 66]; - vec + let vec1 = fill_vec(vec0); + + assert_eq!(vec0, [22, 44, 66]); + assert_eq!(vec1, [22, 44, 66, 88]); + } } diff --git a/exercises/06_move_semantics/move_semantics3.rs b/exercises/06_move_semantics/move_semantics3.rs index 7af9e69..11dbbbe 100644 --- a/exercises/06_move_semantics/move_semantics3.rs +++ b/exercises/06_move_semantics/move_semantics3.rs @@ -1,24 +1,22 @@ -// move_semantics3.rs -// -// Make me compile without adding new lines -- just changing existing lines! (no -// lines with multiple semicolons necessary!) -// -// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand -// for a hint. +// TODO: Fix the compiler error in the function without adding any new line. +fn fill_vec(vec: Vec<i32>) -> Vec<i32> { + vec.push(88); -// I AM NOT DONE + vec +} -#[test] fn main() { - let vec0 = vec![22, 44, 66]; - - let mut vec1 = fill_vec(vec0); - - assert_eq!(vec1, vec![22, 44, 66, 88]); + // You can optionally experiment here. } -fn fill_vec(vec: Vec<i32>) -> Vec<i32> { - vec.push(88); +#[cfg(test)] +mod tests { + use super::*; - vec + #[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/exercises/06_move_semantics/move_semantics4.rs b/exercises/06_move_semantics/move_semantics4.rs index 80b49db..83a0344 100644 --- a/exercises/06_move_semantics/move_semantics4.rs +++ b/exercises/06_move_semantics/move_semantics4.rs @@ -1,29 +1,18 @@ -// move_semantics4.rs -// -// Refactor this code so that instead of passing `vec0` into the `fill_vec` -// function, the Vector gets created in the function itself and passed back to -// the main function. -// -// 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); - - assert_eq!(vec1, vec![22, 44, 66, 88]); + // You can optionally experiment here. } -// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this! -fn fill_vec() -> Vec<i32> { - // Instead, let's create and fill the Vec in here - how do you do that? - let mut vec = vec; - - vec.push(88); - - vec +#[cfg(test)] +mod tests { + // TODO: Fix the compiler errors only by reordering the lines in the test. + // Don't add, change or remove any line. + #[test] + fn move_semantics4() { + let mut x = 100; + let y = &mut x; + let z = &mut x; + *y += 100; + *z += 1000; + assert_eq!(x, 1200); + } } diff --git a/exercises/06_move_semantics/move_semantics5.rs b/exercises/06_move_semantics/move_semantics5.rs index 267bdcc..fc59338 100644 --- a/exercises/06_move_semantics/move_semantics5.rs +++ b/exercises/06_move_semantics/move_semantics5.rs @@ -1,19 +1,24 @@ -// move_semantics5.rs -// -// Make me compile only by reordering the lines in `main()`, but without adding, -// changing or removing any of them. -// -// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand -// for a hint. +#![allow(clippy::ptr_arg)] -// I AM NOT DONE +// TODO: Fix the compiler errors without changing anything except adding or +// removing references (the character `&`). -#[test] fn main() { - let mut x = 100; - let y = &mut x; - let z = &mut x; - *y += 100; - *z += 1000; - assert_eq!(x, 1200); + let data = "Rust is great!".to_string(); + + get_char(data); + + string_uppercase(&data); +} + +// Shouldn't take ownership +fn get_char(data: String) -> char { + data.chars().last().unwrap() +} + +// Should take ownership +fn string_uppercase(mut data: &String) { + data = data.to_uppercase(); + + println!("{data}"); } diff --git a/exercises/06_move_semantics/move_semantics6.rs b/exercises/06_move_semantics/move_semantics6.rs deleted file mode 100644 index cace4ca..0000000 --- a/exercises/06_move_semantics/move_semantics6.rs +++ /dev/null @@ -1,28 +0,0 @@ -// move_semantics6.rs -// -// You can't change anything except adding or removing references. -// -// 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); - - string_uppercase(&data); -} - -// Should not take ownership -fn get_char(data: String) -> char { - data.chars().last().unwrap() -} - -// Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); - - println!("{}", data); -} |
