Remove unnecessary cloning and conversions. Expand tabs left in examples.
fn main() {
let args: Vec<String> = env::args().collect();
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.");
let mut opts = Options::new();
opts.optflag("h", "help", "Show this usage message.");
print_usage(&program, opts);
return;
}
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
+ // Do stuff with information
```rust,ignore
use std::fs::File;
```rust,ignore
use std::fs::File;
// 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
// 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
fn main() {
let args: Vec<String> = env::args().collect();
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.");
let mut opts = Options::new();
opts.optflag("h", "help", "Show this usage message.");
if matches.opt_present("h") {
print_usage(&program, opts);
if matches.opt_present("h") {
print_usage(&program, opts);
- 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);
+ let file = File::open(data_path).unwrap();
+ let mut rdr = csv::Reader::from_reader(file);
- for row in rdr.decode::<Row>() {
- let row = row.unwrap();
+ for row in rdr.decode::<Row>() {
+ let row = row.unwrap();
- if row.city == city {
- println!("{}, {}: {:?}",
- row.city, row.country,
- row.population.expect("population count"));
- }
- }
+ if row.city == city {
+ println!("{}, {}: {:?}",
+ row.city, row.country,
+ row.population.expect("population count"));
+ }
+ }
simply ignoring that row.
```rust,ignore
simply ignoring that row.
```rust,ignore
struct Row {
// unchanged
}
struct Row {
// unchanged
}
- let args: Vec<String> = env::args().collect();
- let program = args[0].clone();
+ let args: Vec<String> = env::args().collect();
+ let program = &args[0];
- let mut opts = Options::new();
- opts.optflag("h", "help", "Show this usage message.");
+ let mut opts = Options::new();
+ opts.optflag("h", "help", "Show this usage message.");
- let matches = match opts.parse(&args[1..]) {
- Ok(m) => { m }
- Err(e) => { panic!(e.to_string()) }
- };
- if matches.opt_present("h") {
- print_usage(&program, opts);
- return;
- }
+ let matches = match opts.parse(&args[1..]) {
+ Ok(m) => { m }
+ Err(e) => { panic!(e.to_string()) }
+ };
+ if matches.opt_present("h") {
+ print_usage(&program, opts);
+ 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) {
- println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
- }
+ let data_path = &args[1];
+ let city = &args[2];
+ for pop in search(data_path, city) {
+ println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
+ }
```rust,ignore
fn print_usage(program: &str, opts: Options) {
```rust,ignore
fn print_usage(program: &str, opts: Options) {
- println!("{}", opts.usage(&format!("Usage: {} [options] <city>", program)));
+ println!("{}", opts.usage(&format!("Usage: {} [options] <city>", program)));
}
```
The next part is going to be only a little harder:
}
```
The next part is going to be only a little harder:
opts.optflag("h", "help", "Show this usage message.");
...
let file = matches.opt_str("f");
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() {
let city = if !matches.free.is_empty() {
- matches.free[0].clone()
- print_usage(&program, opts);
- return;
+ 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);
Ok(pops) => {
for pop in pops {
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);