Skip to content

Compare changes

Choose two branches to see what one has that the other does not, then open a pull request for it.

base
compare
Open a pull request

3 commits

4 files+33−150/4 viewed
+1−0
1717 ### Changed
1818
1919 - Renamed the public `greet()` function to `hail()` and `farewell()` to `part()`
20+- Renamed `hail()` back to `greet()` and `part()` to `farewell()`
+1−0
1515 | `--lang LANG` | Greets in the given language. Supported values are `en`, `es` and `fr`. Defaults to `en`. |
1616 | `--loud`, `-L` | Ends the greeting with `!!!` instead of `!`. |
1717 | `--bye` | Prints `Goodbye, NAME!` after the greeting. |
18+| `--both` | Prints the greeting and `Goodbye, NAME!` on one line. |
1819 | `--wave` | Prints `o/` after the greeting. |
1920 | `--quiet` | Prints no greeting at all. `--version` still prints the version. |
2021
+24−13
4646 pub bye: bool,
4747 /// Whether to print a wave after the greeting (`--wave`).
4848 pub wave: bool,
49+ /// Whether to print the greeting and a goodbye on one line (`--both`).
50+ pub both: bool,
4951 }
5052
5153 impl Default for Options {
6264 loud: false,
6365 bye: false,
6466 wave: false,
67+ both: false,
6568 }
6669 }
6770 }
99102 options.bye = true;
100103 } else if arg == "--wave" {
101104 options.wave = true;
105+ } else if arg == "--both" {
106+ options.both = true;
102107 } else if arg == "--times" {
103108 let value = args
104109 .next()
141146 }
142147
143148 /// The greeting for `name`.
144−pub fn hail(name: &str) -> String {
149+pub fn greet(name: &str) -> String {
145150 greeting_in(name, Lang::En)
146151 }
147152
162167 }
163168
164169 /// The farewell for `name`.
165−pub fn part(name: &str) -> String {
170+pub fn farewell(name: &str) -> String {
166171 let trimmed = name.trim();
167172 let display_name = if trimmed.is_empty() { "world" } else { trimmed };
168173 format!("Goodbye, {display_name}!")
195200 }
196201
197202 #[test]
203+ fn parse_args_both() {
204+ assert!(parse_args(args(&["--both", "Ada"])).unwrap().both);
205+ assert!(!parse_args(args(&["Ada"])).unwrap().both);
206+ }
207+
208+ #[test]
198209 fn wave_is_o_slash() {
199210 assert_eq!(wave(), "o/");
200211 }
208219
209220 #[test]
210221 fn loud_uses_three_exclamation_marks() {
211− assert_eq!(loud(&hail("Ada")), "Hi, Ada!!!");
222+ assert_eq!(loud(&greet("Ada")), "Hi, Ada!!!");
212223 }
213224
214225 #[test]
339350
340351 #[test]
341352 fn shout_upper_cases_greeting() {
342− assert_eq!(shout(&hail("Ada")), "HI, ADA!");
353+ assert_eq!(shout(&greet("Ada")), "HI, ADA!");
343354 }
344355
345356 #[test]
346357 fn whisper_lower_cases_and_trails_off() {
347− assert_eq!(whisper(&hail("Ada")), "hi, ada...");
358+ assert_eq!(whisper(&greet("Ada")), "hi, ada...");
348359 assert_eq!(whisper(&greeting_in("Luc", Lang::Fr)), "bonjour, luc...");
349360 }
350361
366377
367378 #[test]
368379 fn reverse_spells_backwards() {
369− assert_eq!(hail(&reverse("Ada")), "Hi, adA!");
380+ assert_eq!(greet(&reverse("Ada")), "Hi, adA!");
370381 }
371382
372383 #[test]
373384 fn greets_by_name() {
374− assert_eq!(hail("Ada"), "Hi, Ada!");
385+ assert_eq!(greet("Ada"), "Hi, Ada!");
375386 }
376387
377388 #[test]
378389 fn greets_world_with_empty_string() {
379− assert_eq!(hail(""), "Hi, world!");
390+ assert_eq!(greet(""), "Hi, world!");
380391 }
381392
382393 #[test]
383394 fn greets_world_with_whitespace_only() {
384− assert_eq!(hail(" "), "Hi, world!");
395+ assert_eq!(greet(" "), "Hi, world!");
385396 }
386397
387398 #[test]
388399 fn trims_whitespace_from_name() {
389− assert_eq!(hail(" Ada "), "Hi, Ada!");
400+ assert_eq!(greet(" Ada "), "Hi, Ada!");
390401 }
391402
392403 #[test]
393404 fn says_goodbye_by_name() {
394− assert_eq!(part("Ada"), "Goodbye, Ada!");
405+ assert_eq!(farewell("Ada"), "Goodbye, Ada!");
395406 }
396407
397408 #[test]
398409 fn says_goodbye_to_world_when_blank() {
399− assert_eq!(part(" "), "Goodbye, world!");
410+ assert_eq!(farewell(" "), "Goodbye, world!");
400411 }
401412
402413 #[test]
403414 fn farewell_trims_whitespace() {
404− assert_eq!(part(" Ada "), "Goodbye, Ada!");
415+ assert_eq!(farewell(" Ada "), "Goodbye, Ada!");
405416 }
406417 }
+7−2
11 use std::{env, process};
22
3−use hello::{greeting_in, loud, parse_args, part, reverse, shout, wave, whisper};
3+use hello::{farewell, greeting_in, loud, parse_args, reverse, shout, wave, whisper};
44
55 fn main() {
66 let options = match parse_args(env::args().skip(1)) {
4040 } else {
4141 message
4242 };
43+ let line = if options.both {
44+ format!("{line} {}", farewell(&name))
45+ } else {
46+ line
47+ };
4348 for _ in 0..options.times {
4449 println!("{line}");
4550 }
4752 println!("{}", wave());
4853 }
4954 if options.bye {
50− println!("{}", part(&name));
55+ println!("{}", farewell(&name));
5156 }
5257 }