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 | ||
| Add farewell function alongside greeting | 10 | /// The farewell for `name`. |
| 11 | pub fn farewell(name: &str) -> String { | |
| 12 | let trimmed = name.trim(); | |
| 13 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 14 | format!("Goodbye, {display_name}!") | |
| 15 | } | |
| 16 | ||
| Greet people from the command line | 17 | #[cfg(test)] |
| 18 | mod tests { | |
| 19 | use super::*; | |
| 20 | ||
| 21 | #[test] | |
| 22 | fn greets_by_name() { | |
| 23 | assert_eq!(greeting("Ada"), "Hello, Ada!"); | |
| 24 | } | |
| Handle empty and whitespace-only names, trim whitespace from input | 25 | |
| 26 | #[test] | |
| 27 | fn greets_world_with_empty_string() { | |
| 28 | assert_eq!(greeting(""), "Hello, world!"); | |
| 29 | } | |
| 30 | ||
| 31 | #[test] | |
| 32 | fn greets_world_with_whitespace_only() { | |
| 33 | assert_eq!(greeting(" "), "Hello, world!"); | |
| 34 | } | |
| 35 | ||
| 36 | #[test] | |
| 37 | fn trims_whitespace_from_name() { | |
| 38 | assert_eq!(greeting(" Ada "), "Hello, Ada!"); | |
| 39 | } | |
| Add farewell function alongside greeting | 40 | |
| 41 | #[test] | |
| 42 | fn says_goodbye_by_name() { | |
| 43 | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); | |
| 44 | } | |
| 45 | ||
| 46 | #[test] | |
| 47 | fn says_goodbye_to_world_when_blank() { | |
| 48 | assert_eq!(farewell(" "), "Goodbye, world!"); | |
| 49 | } | |
| 50 | ||
| 51 | #[test] | |
| 52 | fn farewell_trims_whitespace() { | |
| 53 | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); | |
| 54 | } | |
| Greet people from the command line | 55 | } |