Approved by g1t-agent
For issueGreet in Spanish and French#21 of 2 pull requestsmain has moved since this pull request was made. It has to catch up before it can merge.
Merge main into the pull request's fork and push.
Other work is changing the same files
Whichever merges second will have to catch up, and may conflict.
| 1 | + | # This file is automatically @generated by Cargo. | |
| 2 | + | # It is not intended for manual editing. | |
| 3 | + | version = 4 | |
| 4 | + | ||
| 5 | + | [[package]] | |
| 6 | + | name = "hello" | |
| 7 | + | version = "0.1.0" |
| 1 | 1 | //! Greetings. | |
| 2 | 2 | ||
| 3 | - | /// The greeting for `name`. | |
| 4 | - | pub fn greeting(name: &str) -> String { | |
| 5 | - | format!("Hello, {name}!") | |
| 3 | + | /// The greeting for `name` in the given language. | |
| 4 | + | /// Supported languages: "en" (English), "es" (Spanish), "fr" (French). | |
| 5 | + | /// Returns None for unknown languages. | |
| 6 | + | pub fn greeting(name: &str, lang: &str) -> Option<String> { | |
| 7 | + | match lang { | |
| 8 | + | "en" => Some(format!("Hello, {name}!")), | |
| 9 | + | "es" => Some(format!("¡Hola, {name}!")), | |
| 10 | + | "fr" => Some(format!("Bonjour, {name} !")), | |
| 11 | + | _ => None, | |
| 12 | + | } | |
| 6 | 13 | } | |
| 7 | 14 | ||
| 8 | 15 | #[cfg(test)] | |
| 11 | 18 | ||
| 12 | 19 | #[test] | |
| 13 | 20 | fn greets_by_name() { | |
| 14 | - | assert_eq!(greeting("Ada"), "Hello, Ada!"); | |
| 21 | + | assert_eq!(greeting("Ada", "en"), Some("Hello, Ada!".to_string())); | |
| 22 | + | } | |
| 23 | + | ||
| 24 | + | #[test] | |
| 25 | + | fn greets_in_spanish() { | |
| 26 | + | assert_eq!(greeting("Ana", "es"), Some("¡Hola, Ana!".to_string())); | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | #[test] | |
| 30 | + | fn greets_in_french() { | |
| 31 | + | assert_eq!(greeting("Luc", "fr"), Some("Bonjour, Luc !".to_string())); | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | #[test] | |
| 35 | + | fn unknown_language_returns_none() { | |
| 36 | + | assert_eq!(greeting("Ada", "de"), None); | |
| 15 | 37 | } | |
| 16 | 38 | } |
| 1 | 1 | use std::env; | |
| 2 | + | use std::process; | |
| 2 | 3 | ||
| 3 | 4 | use hello::greeting; | |
| 4 | 5 | ||
| 5 | 6 | fn main() { | |
| 6 | - | // The name given, or whoever is signed in. | |
| 7 | - | let name = env::args() | |
| 8 | - | .nth(1) | |
| 7 | + | let mut args = env::args().skip(1); | |
| 8 | + | let mut lang = "en".to_string(); | |
| 9 | + | let mut name = None; | |
| 10 | + | ||
| 11 | + | // Parse arguments | |
| 12 | + | while let Some(arg) = args.next() { | |
| 13 | + | if arg == "--lang" { | |
| 14 | + | lang = match args.next() { | |
| 15 | + | Some(l) => l, | |
| 16 | + | None => { | |
| 17 | + | eprintln!("Error: --lang requires an argument"); | |
| 18 | + | process::exit(1); | |
| 19 | + | } | |
| 20 | + | }; | |
| 21 | + | } else { | |
| 22 | + | name = Some(arg); | |
| 23 | + | break; | |
| 24 | + | } | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | // Get the name, or fall back to USER env var, or "world" | |
| 28 | + | let name = name | |
| 9 | 29 | .or_else(|| env::var("USER").ok()) | |
| 10 | 30 | .unwrap_or_else(|| "world".to_owned()); | |
| 11 | - | println!("{}", greeting(&name)); | |
| 31 | + | ||
| 32 | + | // Get the greeting, or print error and exit | |
| 33 | + | match greeting(&name, &lang) { | |
| 34 | + | Some(msg) => println!("{}", msg), | |
| 35 | + | None => { | |
| 36 | + | eprintln!("Error: unknown language '{}'", lang); | |
| 37 | + | eprintln!("Supported languages: en, es, fr"); | |
| 38 | + | process::exit(1); | |
| 39 | + | } | |
| 40 | + | } | |
| 12 | 41 | } |