
π This article includes 1 downloadable practice file β
fn parse(line: &str) -> Result<(String, f64), RowError> {
let f: Vec<&str> = line.split(',').collect();
if f.len() < 5 { return Err(RowError::Missing); }
let qty: f64 = f[3].trim().parse().map_err(|_| RowError::BadNumber(f[3].to_string()))?;
let rate: f64 = f[4].trim().parse().map_err(|_| RowError::BadNumber(f[4].to_string()))?;
Ok((f[1].to_string(), qty * rate))
}
Functions that can fail return Result<T, E>: Ok(value) or Err(error). The ? operator returns the error early. Option<T> (Some/None) replaces null.
Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.
Practice
Run the project file with a broken line added to sales.csv and watch it skip that row.
π Practice files for this article
- πrust-csv-summary-main.rsLesson code file.β¬ RS Β· 1 KB
Free to use for learning. Files with macros (.bas) are plain text β import them with Alt+F11 β File β Import File, and always test on a copy.
Stuck on a step? Ask a question and the AI answers using this article.