pr_01m3xsjpehe75by7vy5ma74jt2/src/main.rs

34 lines927 bytesCodeBlame

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 line1use std::env;
Add --lang option to greet in Spanish and French2use std::process;
Greet people from the command line3
4use hello::greeting;
5
6fn main() {
Add --lang option to greet in Spanish and French7 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 line23 // The name given, or whoever is signed in.
Add --lang option to greet in Spanish and French24 let name = name
Greet people from the command line25 .or_else(|| env::var("USER").ok())
26 .unwrap_or_else(|| "world".to_owned());
Add --lang option to greet in Spanish and French27 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 line34}