Skip to content

Commit 422cf2d

Browse files
committed
Clean up Error Handling case study examples
Remove unnecessary cloning and conversions. Expand tabs left in examples.
1 parent f50fb15 commit 422cf2d

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

src/doc/book/error-handling.md

+44-45
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ fn print_usage(program: &str, opts: Options) {
15921592
15931593
fn main() {
15941594
let args: Vec<String> = env::args().collect();
1595-
let program = args[0].clone();
1595+
let program = &args[0];
15961596
15971597
let mut opts = Options::new();
15981598
opts.optflag("h", "help", "Show this usage message.");
@@ -1605,10 +1605,10 @@ fn main() {
16051605
print_usage(&program, opts);
16061606
return;
16071607
}
1608-
let data_path = args[1].clone();
1609-
let city = args[2].clone();
1608+
let data_path = &args[1];
1609+
let city = &args[2];
16101610
1611-
// Do stuff with information
1611+
// Do stuff with information
16121612
}
16131613
```
16141614

@@ -1640,7 +1640,6 @@ sure to add `extern crate csv;` to the top of your file.)
16401640

16411641
```rust,ignore
16421642
use std::fs::File;
1643-
use std::path::Path;
16441643
16451644
// This struct represents the data in each row of the CSV file.
16461645
// Type based decoding absolves us of a lot of the nitty gritty error
@@ -1666,7 +1665,7 @@ fn print_usage(program: &str, opts: Options) {
16661665
16671666
fn main() {
16681667
let args: Vec<String> = env::args().collect();
1669-
let program = args[0].clone();
1668+
let program = &args[0];
16701669
16711670
let mut opts = Options::new();
16721671
opts.optflag("h", "help", "Show this usage message.");
@@ -1678,25 +1677,24 @@ fn main() {
16781677
16791678
if matches.opt_present("h") {
16801679
print_usage(&program, opts);
1681-
return;
1682-
}
1680+
return;
1681+
}
16831682
1684-
let data_file = args[1].clone();
1685-
let data_path = Path::new(&data_file);
1686-
let city = args[2].clone();
1683+
let data_path = &args[1];
1684+
let city: &str = &args[2];
16871685
1688-
let file = File::open(data_path).unwrap();
1689-
let mut rdr = csv::Reader::from_reader(file);
1686+
let file = File::open(data_path).unwrap();
1687+
let mut rdr = csv::Reader::from_reader(file);
16901688
1691-
for row in rdr.decode::<Row>() {
1692-
let row = row.unwrap();
1689+
for row in rdr.decode::<Row>() {
1690+
let row = row.unwrap();
16931691
1694-
if row.city == city {
1695-
println!("{}, {}: {:?}",
1696-
row.city, row.country,
1697-
row.population.expect("population count"));
1698-
}
1699-
}
1692+
if row.city == city {
1693+
println!("{}, {}: {:?}",
1694+
row.city, row.country,
1695+
row.population.expect("population count"));
1696+
}
1697+
}
17001698
}
17011699
```
17021700

@@ -1745,6 +1743,8 @@ Note that we opt to handle the possibility of a missing population count by
17451743
simply ignoring that row.
17461744

17471745
```rust,ignore
1746+
use std::path::Path;
1747+
17481748
struct Row {
17491749
// unchanged
17501750
}
@@ -1782,27 +1782,26 @@ fn search<P: AsRef<Path>>(file_path: P, city: &str) -> Vec<PopulationCount> {
17821782
}
17831783
17841784
fn main() {
1785-
let args: Vec<String> = env::args().collect();
1786-
let program = args[0].clone();
1785+
let args: Vec<String> = env::args().collect();
1786+
let program = &args[0];
17871787
1788-
let mut opts = Options::new();
1789-
opts.optflag("h", "help", "Show this usage message.");
1788+
let mut opts = Options::new();
1789+
opts.optflag("h", "help", "Show this usage message.");
17901790
1791-
let matches = match opts.parse(&args[1..]) {
1792-
Ok(m) => { m }
1793-
Err(e) => { panic!(e.to_string()) }
1794-
};
1795-
if matches.opt_present("h") {
1796-
print_usage(&program, opts);
1797-
return;
1798-
}
1791+
let matches = match opts.parse(&args[1..]) {
1792+
Ok(m) => { m }
1793+
Err(e) => { panic!(e.to_string()) }
1794+
};
1795+
if matches.opt_present("h") {
1796+
print_usage(&program, opts);
1797+
return;
1798+
}
17991799
1800-
let data_file = args[1].clone();
1801-
let data_path = Path::new(&data_file);
1802-
let city = args[2].clone();
1803-
for pop in search(&data_path, &city) {
1804-
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
1805-
}
1800+
let data_path = &args[1];
1801+
let city = &args[2];
1802+
for pop in search(data_path, city) {
1803+
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
1804+
}
18061805
}
18071806
18081807
```
@@ -1912,7 +1911,7 @@ First, here's the new usage:
19121911

19131912
```rust,ignore
19141913
fn print_usage(program: &str, opts: Options) {
1915-
println!("{}", opts.usage(&format!("Usage: {} [options] <city>", program)));
1914+
println!("{}", opts.usage(&format!("Usage: {} [options] <city>", program)));
19161915
}
19171916
```
19181917
The next part is going to be only a little harder:
@@ -1924,16 +1923,16 @@ opts.optopt("f", "file", "Choose an input file, instead of using STDIN.", "NAME"
19241923
opts.optflag("h", "help", "Show this usage message.");
19251924
...
19261925
let file = matches.opt_str("f");
1927-
let data_file = file.as_ref().map(Path::new);
1926+
let data_file = &file.as_ref().map(Path::new);
19281927
19291928
let city = if !matches.free.is_empty() {
1930-
matches.free[0].clone()
1929+
&matches.free[0]
19311930
} else {
1932-
print_usage(&program, opts);
1933-
return;
1931+
print_usage(&program, opts);
1932+
return;
19341933
};
19351934
1936-
match search(&data_file, &city) {
1935+
match search(data_file, city) {
19371936
Ok(pops) => {
19381937
for pop in pops {
19391938
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);

0 commit comments

Comments
 (0)