pr_01m3xsjv8netd8ept0sbk2rms3/src/lib.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 | //! Greetings. |
| 2 | ||
| Add multi-language support with --lang option | 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 | } | |
| Greet people from the command line | 13 | } |
| 14 | ||
| 15 | #[cfg(test)] | |
| 16 | mod tests { | |
| 17 | use super::*; | |
| 18 | ||
| 19 | #[test] | |
| 20 | fn greets_by_name() { | |
| Add multi-language support with --lang option | 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); | |
| Greet people from the command line | 37 | } |
| 38 | } |