Last active
November 12, 2017 11:11
-
-
Save gregsifr/7a4c6e9a21519110300691dca647b055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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