Skip to content

Commit f80fbca

Browse files
authored
Merge pull request #2294 from senekor/senekor/qxykzqyxnnwy
Remove use of `map` in early vecs2 exercise
2 parents 1955313 + d8f4b06 commit f80fbca

File tree

4 files changed

+5
-74
lines changed

4 files changed

+5
-74
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
### Changed
4+
5+
- `vecs2`: Removed the use of `map` and `collect`, which are only taught later.
6+
37
## 6.5.0 (2025-08-21)
48

59
### Added

exercises/05_vecs/vecs2.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,6 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
99
output
1010
}
1111

12-
fn vec_map_example(input: &[i32]) -> Vec<i32> {
13-
// An example of collecting a vector after mapping.
14-
// We map each element of the `input` slice to its value plus 1.
15-
// If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`.
16-
input.iter().map(|element| element + 1).collect()
17-
}
18-
19-
fn vec_map(input: &[i32]) -> Vec<i32> {
20-
// TODO: Here, we also want to multiply each element in the `input` slice
21-
// by 2, but with iterator mapping instead of manually pushing into an empty
22-
// vector.
23-
// See the example in the function `vec_map_example` above.
24-
input
25-
.iter()
26-
.map(|element| {
27-
// ???
28-
})
29-
.collect()
30-
}
31-
3212
fn main() {
3313
// You can optionally experiment here.
3414
}
@@ -43,18 +23,4 @@ mod tests {
4323
let ans = vec_loop(&input);
4424
assert_eq!(ans, [4, 8, 12, 16, 20]);
4525
}
46-
47-
#[test]
48-
fn test_vec_map_example() {
49-
let input = [1, 2, 3];
50-
let ans = vec_map_example(&input);
51-
assert_eq!(ans, [2, 3, 4]);
52-
}
53-
54-
#[test]
55-
fn test_vec_map() {
56-
let input = [2, 4, 6, 8, 10];
57-
let ans = vec_map(&input);
58-
assert_eq!(ans, [4, 8, 12, 16, 20]);
59-
}
6026
}

rustlings-macros/info.toml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,7 @@ of the Rust book to learn more."""
318318
name = "vecs2"
319319
dir = "05_vecs"
320320
hint = """
321-
In the first function, we create an empty vector and want to push new elements
322-
to it.
323-
324-
In the second function, we map the values of the input and collect them into
325-
a vector.
326-
327-
After you've completed both functions, decide for yourself which approach you
328-
like better.
329-
330-
What do you think is the more commonly used pattern under Rust developers?"""
321+
Use the `.push()` method on the vector to push new elements to it."""
331322

332323
# MOVE SEMANTICS
333324

solutions/05_vecs/vecs2.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,6 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
88
output
99
}
1010

11-
fn vec_map_example(input: &[i32]) -> Vec<i32> {
12-
// An example of collecting a vector after mapping.
13-
// We map each element of the `input` slice to its value plus 1.
14-
// If the input is `[1, 2, 3]`, the output is `[2, 3, 4]`.
15-
input.iter().map(|element| element + 1).collect()
16-
}
17-
18-
fn vec_map(input: &[i32]) -> Vec<i32> {
19-
// We will dive deeper into iterators, but for now, this is all what you
20-
// had to do!
21-
// Advanced note: This method is more efficient because it automatically
22-
// preallocates enough capacity. This can be done manually in `vec_loop`
23-
// using `Vec::with_capacity(input.len())` instead of `Vec::new()`.
24-
input.iter().map(|element| 2 * element).collect()
25-
}
26-
2711
fn main() {
2812
// You can optionally experiment here.
2913
}
@@ -38,18 +22,4 @@ mod tests {
3822
let ans = vec_loop(&input);
3923
assert_eq!(ans, [4, 8, 12, 16, 20]);
4024
}
41-
42-
#[test]
43-
fn test_vec_map_example() {
44-
let input = [1, 2, 3];
45-
let ans = vec_map_example(&input);
46-
assert_eq!(ans, [2, 3, 4]);
47-
}
48-
49-
#[test]
50-
fn test_vec_map() {
51-
let input = [2, 4, 6, 8, 10];
52-
let ans = vec_map(&input);
53-
assert_eq!(ans, [4, 8, 12, 16, 20]);
54-
}
5525
}

0 commit comments

Comments
 (0)