diff --git a/city-pop/src/main.rs b/city-pop/src/main.rs index 90278a47e5f0a09e82117825269a04e11ef84076..63d0715a6761f8d43c9713c216d6e646cfb7f898 100644 --- a/city-pop/src/main.rs +++ b/city-pop/src/main.rs @@ -4,7 +4,7 @@ extern crate csv; use getopts::Options; -use std::{env,fs}; +use std::{env,fs,io}; use std::error::Error; use std::path::Path; @@ -49,8 +49,11 @@ fn print_usage(program: &str, opts: Options) { fn search<P: AsRef<Path>> (file_path: &Option<P>, city: &str) -> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> { let mut found = vec![]; - let file = try!(fs::File::open(file_path)); - let mut rdr = csv::Reader::from_reader(file); + let input: Box<io::Read> = match *file_path { + None => Box::new(io::stdin()), + Some(ref file_path) => Box::new(try!(fs::File::open(file_path))), + }; + let mut rdr = csv::Reader::from_reader(input); for row in rdr.decode::<Row>() { let row = row.unwrap(); match row.population {