Skip to content

Instantly share code, notes, and snippets.

@gregsifr
Last active November 12, 2017 11:11
Show Gist options
  • Save gregsifr/7a4c6e9a21519110300691dca647b055 to your computer and use it in GitHub Desktop.
Save gregsifr/7a4c6e9a21519110300691dca647b055 to your computer and use it in GitHub Desktop.
extern crate csv;
use std::error::Error;
use std::process;
struct data {
pub time: Vec<String>,
pub name: Vec<String>,
pub score: Vec<String>,
}
fn example(filepath: &str, struct: struct_instance:) {
let mut rdr = csv::Reader::from_reader(filepath);
for result in rdr.records() {
// The iterator yields Result<StringRecord, Error>
let number_of_columns = result.len;
for i in (0..number_of_columns) {
struct_instance[i].push(result[i]); // for each column iterate and insert values into the struct
}
}
}
fn main() {
let filepath = "sample.csv"; // A csv containing the columns: time, name, score
let mut csv_data = struct {time: Vec::new(), name: Vec::new(), score: Vec::new()};
example(filepath, csv_data); //mutates csv_data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment