pr_01m3xsjpehe75by7vy5ma74jt2/src/main.rs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Greet people from the command line | 1 | use std::env; |
| Add --lang option to greet in Spanish and French | 2 | use std::process; |
| Greet people from the command line | 3 | |
| 4 | use hello::greeting; | |
| 5 | ||
| 6 | fn main() { | |
| Add --lang option to greet in Spanish and French | 7 | let mut lang = "en".to_owned(); |
| 8 | let mut name = None; | |
| 9 | let mut args = env::args().skip(1); | |
| 10 | while let Some(arg) = args.next() { | |
| 11 | if arg == "--lang" { | |
| 12 | lang = args.next().unwrap_or_else(|| { | |
| 13 | eprintln!("error: --lang requires a value"); | |
| 14 | process::exit(2); | |
| 15 | }); | |
| 16 | } else if let Some(value) = arg.strip_prefix("--lang=") { | |
| 17 | lang = value.to_owned(); | |
| 18 | } else if name.is_none() { | |
| 19 | name = Some(arg); | |
| 20 | } | |
| 21 | } | |
| 22 | ||
| Greet people from the command line | 23 | // The name given, or whoever is signed in. |
| Add --lang option to greet in Spanish and French | 24 | let name = name |
| Greet people from the command line | 25 | .or_else(|| env::var("USER").ok()) |
| 26 | .unwrap_or_else(|| "world".to_owned()); | |
| Add --lang option to greet in Spanish and French | 27 | match greeting(&name, &lang) { |
| 28 | Ok(text) => println!("{text}"), | |
| 29 | Err(err) => { | |
| 30 | eprintln!("error: {err}"); | |
| 31 | process::exit(2); | |
| 32 | } | |
| 33 | } | |
| Greet people from the command line | 34 | } |