expression
Aggregate a file without retaining every row
For a small trusted numeric dataset, parse each row and update a running total rather than building a collection. Checked arithmetic rejects overflow. For untrusted inputs combine this with the bounded-record reader. Fixture: scratch files {'counts.txt': '3\n5\n7\n'}. Expected stdout (not executed): total=15 Expected stderr: (empty) Expected exit: 0 Common mistake: Collecting all lines first or treating bad numeric records as zero.
Formal Syntax & Structure
rust syntaxrust
BufReader::new(File::open(path)?).lines()Working Example
Example Snippetrust
fn main() { use std::{fs::File, io::{BufReader, BufRead}}; let input = BufReader::new(File::open("counts.txt").unwrap()); let mut total = 0u64; for row in input.lines() { let value: u64 = row.unwrap().parse().unwrap(); total = total.checked_add(value).expect("total overflow"); } println!("total={total}");}