1
Fork 0

Clean up Error Handling case study examples

Remove unnecessary cloning and conversions. Expand tabs left in examples.
This commit is contained in:
Andrew Barchuk 2016-02-07 23:00:01 +02:00
parent f50fb159e9
commit 422cf2d34a

View file

@ -1592,7 +1592,7 @@ fn print_usage(program: &str, opts: Options) {
fn main() {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let program = &args[0];
let mut opts = Options::new();
opts.optflag("h", "help", "Show this usage message.");
@ -1605,8 +1605,8 @@ fn main() {
print_usage(&program, opts);
return;
}
let data_path = args[1].clone();
let city = args[2].clone();
let data_path = &args[1];
let city = &args[2];
// Do stuff with information
}
@ -1640,7 +1640,6 @@ sure to add `extern crate csv;` to the top of your file.)
```rust,ignore
use std::fs::File;
use std::path::Path;
// This struct represents the data in each row of the CSV file.
// Type based decoding absolves us of a lot of the nitty gritty error
@ -1666,7 +1665,7 @@ fn print_usage(program: &str, opts: Options) {
fn main() {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let program = &args[0];
let mut opts = Options::new();
opts.optflag("h", "help", "Show this usage message.");
@ -1681,9 +1680,8 @@ fn main() {
return;
}
let data_file = args[1].clone();
let data_path = Path::new(&data_file);
let city = args[2].clone();
let data_path = &args[1];
let city: &str = &args[2];
let file = File::open(data_path).unwrap();
let mut rdr = csv::Reader::from_reader(file);
@ -1745,6 +1743,8 @@ Note that we opt to handle the possibility of a missing population count by
simply ignoring that row.
```rust,ignore
use std::path::Path;
struct Row {
// unchanged
}
@ -1783,7 +1783,7 @@ fn search<P: AsRef<Path>>(file_path: P, city: &str) -> Vec<PopulationCount> {
fn main() {
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let program = &args[0];
let mut opts = Options::new();
opts.optflag("h", "help", "Show this usage message.");
@ -1797,10 +1797,9 @@ fn main() {
return;
}
let data_file = args[1].clone();
let data_path = Path::new(&data_file);
let city = args[2].clone();
for pop in search(&data_path, &city) {
let data_path = &args[1];
let city = &args[2];
for pop in search(data_path, city) {
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
}
}
@ -1924,16 +1923,16 @@ opts.optopt("f", "file", "Choose an input file, instead of using STDIN.", "NAME"
opts.optflag("h", "help", "Show this usage message.");
...
let file = matches.opt_str("f");
let data_file = file.as_ref().map(Path::new);
let data_file = &file.as_ref().map(Path::new);
let city = if !matches.free.is_empty() {
matches.free[0].clone()
&matches.free[0]
} else {
print_usage(&program, opts);
return;
};
match search(&data_file, &city) {
match search(data_file, city) {
Ok(pops) => {
for pop in pops {
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);