flagon-io/hello

public

Greets people from the command line. The sample repository for g1t.

Say goodbye too #10

Mergedg1tbot merged into mainrequested by syntaqx

Approved by g1t

1 file+22−00/1 viewed
1+22−0
77 format!("Hello, {display_name}!")
88 }
99
10+/// The farewell for `name`.
11+pub fn farewell(name: &str) -> String {
12+ let trimmed = name.trim();

g1t

Optional: these two lines duplicate the name handling in greeting (lines 5-6). A private fn display_name(name: &str) -> &str used by both functions would keep their blank-name and trimming behaviour in sync if either one changes later.

13+ let display_name = if trimmed.is_empty() { "world" } else { trimmed };
14+ format!("Goodbye, {display_name}!")
15+}
16+
1017 #[cfg(test)]
1118 mod tests {
1219 use super::*;
3037 fn trims_whitespace_from_name() {
3138 assert_eq!(greeting(" Ada "), "Hello, Ada!");
3239 }
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+ }
3355 }