hello/src/lib.rs

33 lines716 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 line1//! Greetings.
2
3/// The greeting for `name`.
4pub fn greeting(name: &str) -> String {
Handle empty and whitespace-only names, trim whitespace from input5 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 line8}
9
10#[cfg(test)]
11mod 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 input18
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 line33}