Parsing often combines splitting, trimming, and conversion while ignoring fields that do not match the target type.

Program

Play the program to choose a CSV line and sum the numeric fields.

csv_number_parser.rs
fn main() {
    let line = ;
    let total = parse_numbers(line);
    println!("total={total}");
}

fn parse_numbers(line: &str) -> i32 {
    line.split(',')
        .filter_map(|part| part.trim().parse::<i32>().ok())
        .sum()
}
fn main() {
    let line = ;
    let total = parse_numbers(line);
    println!("total={total}");
}

fn parse_numbers(line: &str) -> i32 {
    line.split(',')
        .filter_map(|part| part.trim().parse::<i32>().ok())
        .sum()
}
fn main() {
    let line = ;
    let total = parse_numbers(line);
    println!("total={total}");
}

fn parse_numbers(line: &str) -> i32 {
    line.split(',')
        .filter_map(|part| part.trim().parse::<i32>().ok())
        .sum()
}
split `split(',')` creates fields from a comma-separated line.
filter_map `filter_map` keeps successfully parsed numbers and skips invalid fields.
sum `sum` folds the parsed numbers into one total.