Read CSV records

    0

    0

    Giovanny Gongora

    Codiga's Rust Recipes

    from a variable with csv content

    use csv::Error;
    
    fn main() -> Result<(), Error> {
      let csv = "year,make,model,description
      1948,Porsche,356,Luxury sports car
      1967,Ford,Mustang fastback 1967,American car";
      let mut reader = csv::Reader::from_reader(csv.as_bytes());
      for record in reader.records() {
        let record = record?;
        println!(
          "In {}, {} built the {} model. It is a {}.",
          &record[0],
          &record[1],
          &record[2],
          &record[3]
        );
      }
      Ok(())
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.