diff options
| author | Mo Bitar <76752051+mo8it@users.noreply.github.com> | 2025-09-25 16:55:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-25 16:55:36 +0200 |
| commit | f80fbca12e47014a314e5e931678529c28cd9fd8 (patch) | |
| tree | 2d25e5ae39659d2394f55d556a844a14153e98f7 | |
| parent | 19553133627bf3d645d8ebfd7fffa0e922073f83 (diff) | |
| parent | d8f4b06c91c54bccf934b84560641da3a7f202a8 (diff) | |
Merge pull request #2294 from senekor/senekor/qxykzqyxnnwy
Remove use of `map` in early vecs2 exercise
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | exercises/05_vecs/vecs2.rs | 34 | ||||
| -rw-r--r-- | rustlings-macros/info.toml | 11 | ||||
| -rw-r--r-- | solutions/05_vecs/vecs2.rs | 30 |
4 files changed, 5 insertions, 74 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 18e0aa6..2d2b415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +### Changed + +- `vecs2`: Removed the use of `map` and `collect`, which are only taught later. + ## 6.5.0 (2025-08-21) ### Added diff --git a/exercises/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs index a9be258..0c99626 100644 --- a/exercises/05_vecs/vecs2.rs +++ b/exercises/05_vecs/vecs2.rs @@ -9,26 +9,6 @@ fn vec_loop(input: &[i32]) -> Vec<i32> { 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 main() { // You can optionally experiment here. } @@ -43,18 +23,4 @@ mod tests { let ans = vec_loop(&input); assert_eq!(ans, [4, 8, 12, 16, 20]); } - - #[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 input = [2, 4, 6, 8, 10]; - let ans = vec_map(&input); - assert_eq!(ans, [4, 8, 12, 16, 20]); - } } diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index 516fd32..ca3ecf1 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -318,16 +318,7 @@ of the Rust book to learn more.""" name = "vecs2" dir = "05_vecs" hint = """ -In the first function, we create an empty vector and want to push new elements -to it. - -In the second function, we map the values of the input and collect them into -a vector. - -After you've completed both functions, decide for yourself which approach you -like better. - -What do you think is the more commonly used pattern under Rust developers?""" +Use the `.push()` method on the vector to push new elements to it.""" # MOVE SEMANTICS diff --git a/solutions/05_vecs/vecs2.rs b/solutions/05_vecs/vecs2.rs index 87f7625..aae7103 100644 --- a/solutions/05_vecs/vecs2.rs +++ b/solutions/05_vecs/vecs2.rs @@ -8,22 +8,6 @@ fn vec_loop(input: &[i32]) -> Vec<i32> { 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> { - // We will dive deeper into iterators, but for now, this is all what you - // had to do! - // Advanced note: This method is more efficient because it automatically - // preallocates enough capacity. This can be done manually in `vec_loop` - // using `Vec::with_capacity(input.len())` instead of `Vec::new()`. - input.iter().map(|element| 2 * element).collect() -} - fn main() { // You can optionally experiment here. } @@ -38,18 +22,4 @@ mod tests { let ans = vec_loop(&input); assert_eq!(ans, [4, 8, 12, 16, 20]); } - - #[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 input = [2, 4, 6, 8, 10]; - let ans = vec_map(&input); - assert_eq!(ans, [4, 8, 12, 16, 20]); - } } |
