Enter your book access code

The curriculum, drills, tutor, and interviewer mode are for readers of Ace the Rust Interview. The code is printed in the Preface (Companion site access).

Don’t have the book yet? The landing page stays open — purchase unlocks this gym.

intermediate · order 183

Collect a fallible iterator

iter-from-01Related book chapters: 9, 8 (optional — this page is complete on its own)iteratorcollectResult

Slug: iter-from-01 · Paired reading: Chapters 9 and 8

Prompt

Implement fn parse_all(lines: &[&str]) -> Result<Vec<i32>, String> that parses each line with str::parse::<i32>(), collecting into Result<Vec<i32>, String>. On the first failure, return Err with that line’s text (owned).

Contract

  • Empty input → Ok(vec![])
  • Stop at first bad token (do not keep parsing)
  • Success preserves order

Predict first

Why does collect into Result<Vec<_>, _> short-circuit?

Rust angle

FromIterator for Result is the idiomatic fallible pipeline — Chapter 8 meets Chapter 9.