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 | ||
| 3 | /// The greeting for `name`. | |
| 4 | pub fn greeting(name: &str) -> String { | |
| Handle empty and whitespace-only names, trim whitespace from input | 5 | let trimmed = name.trim(); |
| 6 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 7 | format!("Hello, {display_name}!") | |
| Greet people from the command line | 8 | } |
| 9 | ||
| 10 | #[cfg(test)] | |
| 11 | mod tests { | |
| 12 | use super::*; | |
| 13 | ||
| 14 | #[test] | |
| 15 | fn greets_by_name() { | |
| 16 | assert_eq!(greeting("Ada"), "Hello, Ada!"); | |
| 17 | } | |
| Handle empty and whitespace-only names, trim whitespace from input | 18 | |
| 19 | #[test] | |
| 20 | fn greets_world_with_empty_string() { | |
| 21 | assert_eq!(greeting(""), "Hello, world!"); | |
| 22 | } | |
| 23 | ||
| 24 | #[test] | |
| 25 | fn greets_world_with_whitespace_only() { | |
| 26 | assert_eq!(greeting(" "), "Hello, world!"); | |
| 27 | } | |
| 28 | ||
| 29 | #[test] | |
| 30 | fn trims_whitespace_from_name() { | |
| 31 | assert_eq!(greeting(" Ada "), "Hello, Ada!"); | |
| 32 | } | |
| Greet people from the command line | 33 | } |