1 use std::{env, error::Error, ffi::OsString, process};
2 
run() -> Result<(), Box<dyn Error>>3 fn run() -> Result<(), Box<dyn Error>> {
4     let file_path = get_first_arg()?;
5     let mut wtr = csv::Writer::from_path(file_path)?;
6 
7     wtr.write_record(&[
8         "City",
9         "State",
10         "Population",
11         "Latitude",
12         "Longitude",
13     ])?;
14     wtr.write_record(&[
15         "Davidsons Landing",
16         "AK",
17         "",
18         "65.2419444",
19         "-165.2716667",
20     ])?;
21     wtr.write_record(&["Kenai", "AK", "7610", "60.5544444", "-151.2583333"])?;
22     wtr.write_record(&["Oakman", "AL", "", "33.7133333", "-87.3886111"])?;
23 
24     wtr.flush()?;
25     Ok(())
26 }
27 
28 /// Returns the first positional argument sent to this process. If there are no
29 /// positional arguments, then this returns an error.
get_first_arg() -> Result<OsString, Box<dyn Error>>30 fn get_first_arg() -> Result<OsString, Box<dyn Error>> {
31     match env::args_os().nth(1) {
32         None => Err(From::from("expected 1 argument, but got none")),
33         Some(file_path) => Ok(file_path),
34     }
35 }
36 
main()37 fn main() {
38     if let Err(err) = run() {
39         println!("{}", err);
40         process::exit(1);
41     }
42 }
43