Skip to content

Commit 8651fa9

Browse files
committed
remove unnecessary .iter() and stringy_nums lines
1 parent 85afd21 commit 8651fa9

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

exercise-book/src/iterators.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,10 @@ fn main() -> Result<(), Box<dyn Error>> {
274274
let f = File::open("../exercise-templates/iterators/numbers.txt")?;
275275
let reader = BufReader::new(f);
276276

277-
let numeric_lines = reader.lines()
277+
let numeric_lines: Vec<i32> = reader.lines()
278278
.filter_map(|line| line.ok())
279279
.filter_map(|line| line.parse::<i32>().ok())
280-
.map(|stringy_num| stringy_num.to_string())
281-
.collect::<Vec<String>>();
280+
.collect::<Vec<i32>>();
282281
println!("{:?}", numeric_lines);
283282

284283
Ok(())
@@ -321,7 +320,7 @@ fn main() -> Result<(), Box<dyn Error>> {
321320

322321
### Step 5: Add the odd numbers
323322

324-
Take the odd numbers, `.collect()` into a vector, and add them using a [.fold()](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.fold).
323+
Take the odd numbers, and add them using a [.fold()](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.fold).
325324

326325
You will probably reach for a `.sum::<i32>()`, but `.fold()`s are common enough in idiomatic Rust that we wanted to showcase one here.
327326

@@ -342,10 +341,7 @@ fn main() -> Result<(), Box<dyn Error>> {
342341
let result = reader.lines()
343342
.filter_map(|line| line.ok())
344343
.filter_map(|line| line.parse::<i32>().ok())
345-
.map(|stringy_num| stringy_num.to_string())
346344
.filter(|num| num % 2 != 0)
347-
.collect::<Vec<i32>>()
348-
.iter()
349345
.fold(0, |acc, elem| acc + elem);
350346
// Also works
351347
//.sum::<i32>();

0 commit comments

Comments
 (0)