@@ -1592,7 +1592,7 @@ fn print_usage(program: &str, opts: Options) {
1592
1592
1593
1593
fn main() {
1594
1594
let args: Vec<String> = env::args().collect();
1595
- let program = args[0].clone() ;
1595
+ let program = & args[0];
1596
1596
1597
1597
let mut opts = Options::new();
1598
1598
opts.optflag("h", "help", "Show this usage message.");
@@ -1605,10 +1605,10 @@ fn main() {
1605
1605
print_usage(&program, opts);
1606
1606
return;
1607
1607
}
1608
- let data_path = args[1].clone() ;
1609
- let city = args[2].clone() ;
1608
+ let data_path = & args[1];
1609
+ let city = & args[2];
1610
1610
1611
- // Do stuff with information
1611
+ // Do stuff with information
1612
1612
}
1613
1613
```
1614
1614
@@ -1640,7 +1640,6 @@ sure to add `extern crate csv;` to the top of your file.)
1640
1640
1641
1641
``` rust,ignore
1642
1642
use std::fs::File;
1643
- use std::path::Path;
1644
1643
1645
1644
// This struct represents the data in each row of the CSV file.
1646
1645
// Type based decoding absolves us of a lot of the nitty gritty error
@@ -1666,7 +1665,7 @@ fn print_usage(program: &str, opts: Options) {
1666
1665
1667
1666
fn main() {
1668
1667
let args: Vec<String> = env::args().collect();
1669
- let program = args[0].clone() ;
1668
+ let program = & args[0];
1670
1669
1671
1670
let mut opts = Options::new();
1672
1671
opts.optflag("h", "help", "Show this usage message.");
@@ -1678,25 +1677,24 @@ fn main() {
1678
1677
1679
1678
if matches.opt_present("h") {
1680
1679
print_usage(&program, opts);
1681
- return;
1682
- }
1680
+ return;
1681
+ }
1683
1682
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];
1687
1685
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);
1690
1688
1691
- for row in rdr.decode::<Row>() {
1692
- let row = row.unwrap();
1689
+ for row in rdr.decode::<Row>() {
1690
+ let row = row.unwrap();
1693
1691
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
+ }
1700
1698
}
1701
1699
```
1702
1700
@@ -1745,6 +1743,8 @@ Note that we opt to handle the possibility of a missing population count by
1745
1743
simply ignoring that row.
1746
1744
1747
1745
``` rust,ignore
1746
+ use std::path::Path;
1747
+
1748
1748
struct Row {
1749
1749
// unchanged
1750
1750
}
@@ -1782,27 +1782,26 @@ fn search<P: AsRef<Path>>(file_path: P, city: &str) -> Vec<PopulationCount> {
1782
1782
}
1783
1783
1784
1784
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];
1787
1787
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.");
1790
1790
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
+ }
1799
1799
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
+ }
1806
1805
}
1807
1806
1808
1807
```
@@ -1912,7 +1911,7 @@ First, here's the new usage:
1912
1911
1913
1912
``` rust,ignore
1914
1913
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)));
1916
1915
}
1917
1916
```
1918
1917
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"
1924
1923
opts.optflag("h", "help", "Show this usage message.");
1925
1924
...
1926
1925
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);
1928
1927
1929
1928
let city = if !matches.free.is_empty() {
1930
- matches.free[0].clone()
1929
+ & matches.free[0]
1931
1930
} else {
1932
- print_usage(&program, opts);
1933
- return;
1931
+ print_usage(&program, opts);
1932
+ return;
1934
1933
};
1935
1934
1936
- match search(& data_file, & city) {
1935
+ match search(data_file, city) {
1937
1936
Ok(pops) => {
1938
1937
for pop in pops {
1939
1938
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
0 commit comments