Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 25, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/cd30b13d160daf27709a75bae4f56130 to your computer and use it in GitHub Desktop.
Save rust-play/cd30b13d160daf27709a75bae4f56130 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
extern crate csv;
use std::fs::File;
use std::io;
pub struct RowIter<'a, T: 'a> {
iter: &'a ::csv::StringRecordsIter<'a, T>,
}
pub struct CsvDataTable<R> {
reader: csv::Reader<R>,
}
impl CsvDataTable<File> {
pub fn open_with_table(
path: &str,
) -> Result<CsvDataTable<File>, ::io::Error> {
let csv_file_reader = File::open(path)
.expect("failed to open csv file for reading");
let csv_reader = csv::ReaderBuilder::new()
.from_reader(csv_file_reader);
Ok(CsvDataTable {
reader: csv_reader,
})
}
pub fn rows<'c>(&'c mut self) -> RowIter<'c, File> {
RowIter {
iter: &self.reader.records(),
}
}
}
fn main () {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment