diff options
| author | mo8it <mo8it@proton.me> | 2024-07-03 15:26:35 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-07-03 15:26:35 +0200 |
| commit | f5ce4cf0a50e88e0e70d90e139a6e981791c9af0 (patch) | |
| tree | b908688a611c6e64ff1bb7a6a94e9547b7b575de /exercises/05_vecs | |
| parent | 888ad35d10e8bc6832c11fd8268697311497c1c9 (diff) | |
| parent | ff3e6c05a52aa0c7e558d86404cfe8495a4412fd (diff) | |
Merge branch 'v6'
Diffstat (limited to 'exercises/05_vecs')
| -rw-r--r-- | exercises/05_vecs/vecs1.rs | 24 | ||||
| -rw-r--r-- | exercises/05_vecs/vecs2.rs | 72 |
2 files changed, 51 insertions, 45 deletions
diff --git a/exercises/05_vecs/vecs1.rs b/exercises/05_vecs/vecs1.rs index 65b7a7f..68e1aff 100644 --- a/exercises/05_vecs/vecs1.rs +++ b/exercises/05_vecs/vecs1.rs @@ -1,21 +1,17 @@ -// vecs1.rs -// -// Your task is to create a `Vec` which holds the exact same elements as in the -// array `a`. -// -// Make me compile and pass the test! -// -// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. - -// I AM NOT DONE - fn array_and_vec() -> ([i32; 4], Vec<i32>) { - let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let a = [10, 20, 30, 40]; // Array + + // TODO: Create a vector called `v` which contains the exact same elements as in the array `a`. + // Use the vector macro. + // let v = ???; (a, v) } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; @@ -23,6 +19,6 @@ mod tests { #[test] fn test_array_and_vec_similarity() { let (a, v) = array_and_vec(); - assert_eq!(a, v[..]); + assert_eq!(a, *v); } } diff --git a/exercises/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs index e92c970..a9be258 100644 --- a/exercises/05_vecs/vecs2.rs +++ b/exercises/05_vecs/vecs2.rs @@ -1,31 +1,36 @@ -// vecs2.rs -// -// A Vec of even numbers is given. Your task is to complete the loop so that -// each number in the Vec is multiplied by 2. -// -// Make me pass the test! -// -// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. - -// I AM NOT DONE - -fn vec_loop(mut v: Vec<i32>) -> Vec<i32> { - for element in v.iter_mut() { - // TODO: Fill this up so that each element in the Vec `v` is - // multiplied by 2. - ??? +fn vec_loop(input: &[i32]) -> Vec<i32> { + let mut output = Vec::new(); + + for element in input { + // TODO: Multiply each element in the `input` slice by 2 and push it to + // the `output` vector. } - // At this point, `v` should be equal to [4, 8, 12, 16, 20]. - v + output +} + +fn vec_map_example(input: &[i32]) -> Vec<i32> { + // An example of collecting a vector after mapping. + // We map each element of the `input` slice to its value plus 1. + // If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`. + input.iter().map(|element| element + 1).collect() +} + +fn vec_map(input: &[i32]) -> Vec<i32> { + // TODO: Here, we also want to multiply each element in the `input` slice + // by 2, but with iterator mapping instead of manually pushing into an empty + // vector. + // See the example in the function `vec_map_example` above. + input + .iter() + .map(|element| { + // ??? + }) + .collect() } -fn vec_map(v: &Vec<i32>) -> Vec<i32> { - v.iter().map(|element| { - // TODO: Do the same thing as above - but instead of mutating the - // Vec, you can just return the new number! - ??? - }).collect() +fn main() { + // You can optionally experiment here. } #[cfg(test)] @@ -34,17 +39,22 @@ mod tests { #[test] fn test_vec_loop() { - let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect(); - let ans = vec_loop(v.clone()); + let input = [2, 4, 6, 8, 10]; + let ans = vec_loop(&input); + assert_eq!(ans, [4, 8, 12, 16, 20]); + } - assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>()); + #[test] + fn test_vec_map_example() { + let input = [1, 2, 3]; + let ans = vec_map_example(&input); + assert_eq!(ans, [2, 3, 4]); } #[test] fn test_vec_map() { - let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect(); - let ans = vec_map(&v); - - assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>()); + let input = [2, 4, 6, 8, 10]; + let ans = vec_map(&input); + assert_eq!(ans, [4, 8, 12, 16, 20]); } } |
