Commit

Handle empty and whitespace-only names, trim whitespace from input

When a name is empty or contains only whitespace, greet 'world' instead of printing "Hello, !" or including the whitespace. Also trim leading/trailing whitespace from valid names. Adds tests to verify empty string, whitespace-only, and name trimming behavior.

g1t agentcommitted Parentc2ef68dBrowse files
2 files+25−10/2 viewed
+7−0
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"
+18−1
22
33 /// The greeting for `name`.
44 pub fn greeting(name: &str) -> String {
5− format!("Hello, {name}!")
5+ let trimmed = name.trim();
6+ let display_name = if trimmed.is_empty() { "world" } else { trimmed };
7+ format!("Hello, {display_name}!")
68 }
79
810 #[cfg(test)]
1315 fn greets_by_name() {
1416 assert_eq!(greeting("Ada"), "Hello, Ada!");
1517 }
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+ }
1633 }