Skip to content

Instantly share code, notes, and snippets.

@fancellu
Last active February 24, 2024 02:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/184f36c18322016d6fe4af71fead4240 to your computer and use it in GitHub Desktop.
Save fancellu/184f36c18322016d6fe4af71fead4240 to your computer and use it in GitHub Desktop.
Rust simple CSV processing from a CV file, using csv crate
use std::{error::Error, process};
fn cities() -> Result<(), Box<dyn Error>> {
let mut csv_rdr = csv::Reader::from_path("data/cities.csv")?;
for result in csv_rdr.records() {
let record = result?;
println!("{:?}", record);
println!("{}",record.get(0).unwrap());
}
Ok(())
}
fn main() {
if let Err(err) = cities() {
println!("error running example: {}", err);
process::exit(1);
}
}
@fancellu
Copy link
Author

fancellu commented Feb 20, 2024

Output

StringRecord(["Tokyo", "Kantō", "Japan", "37339000"])
Tokyo
StringRecord(["Delhi", "National Capital Territory", "India", "29341000"])
Delhi
StringRecord(["Shanghai", "Huadong", "China", "26446000"])
Shanghai
StringRecord(["São Paulo", "Southeast", "Brazil", "12325000"])
São Paulo
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment