error-concept

Keep successes and errors in batch parsing

Process every input when partial success is acceptable. Keep the failing input and its position for diagnosis instead of silently discarding errors with filter_map(Result::ok). The example stores original text rather than compiler-dependent error messages. Expected stdout (not executed): [12, 9] [(1, "bad")] Common mistake: Treating invalid numbers as zero or dropping errors without a stated policy.

Formal Syntax & Structure

rust syntaxrust
match Result<T, E> { Ok(value) => ..., Err(error) => ... }

Working Example

Example Snippetrust
fn main() {    let mut valid = Vec::new();    let mut invalid = Vec::new();    for (i, text) in ["12", "bad", "9"].into_iter().enumerate() {        match text.parse::<u16>() { Ok(n) => valid.push(n), Err(_) => invalid.push((i, text)) }    }    println!("{valid:?} {invalid:?}");}
Rust Reference