Lesson 3.2 · 9 min
Choosing with match
An if picks between two roads. Chatterbox needs more: /help should show help, /quit should say goodbye, and everything else should be echoed. For many roads Rust gives you match.
Adds to ChatterboxCommands: /help, /quit.
Arms
A match takes one value and lays out a list of arms. Each arm is a pattern (a description of what the value might look like, such as one exact piece of text), an arrow =>, and what to do. Rust tries the arms from the top and runs the first one that fits.
$ cargo run
commands: /help, /quitThe first two arms are exact pieces of text. The last arm is just a name, other. A bare name matches anything, and gives that name to the value so you can use it. That is what makes it the "everything else" arm. Try "hello" as the message.
Why the last arm is not optional
Rust makes you cover every possible value. Text can be anything, so with only two arms, what should happen for "hello"? The compiler will not let you leave that undecided:
error[E0004]: non-exhaustive patterns: `&_` not covered --> src/main.rs:3:11 | 3 | match message { | ^^^^^^^ pattern `&_` not covered | = note: the matched value is of type `&str` = note: `&str` cannot be matched exhaustively, so a wildcard `_` is necessary help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | 5 ~ "/quit" => println!("bye!"), 6 ~ &_ => todo!(), |
"non-exhaustive patterns" is the compiler saying: there are values you did not handle. It even suggests a catch-all arm (it prints &_ here, because the value is a &str; _ or a name like other do the same job). The underscore is a catch-all without a name, for when you do not need the value.
This is a gift, not a nuisance. In a chat program that grows a new command next month, the compiler will point at every match that forgot it.
What does this print?
helpecho /Helpquit
Reveal the answer
The program prints: echo /Help Text patterns match exactly, capital letters included. "/Help" is not "/help", so the first two arms are skipped and the catch-all takes it.
Chatterbox v0.3: its first commands
Chatterbox learns its first two commands, and the if in main from lesson 3.1 becomes a match. First, a new constant at the top, next to MAX_LEN: const HELP: &str = "commands: /help, /quit";. The help text is a fixed fact, so it gets a name in capitals like the other constants from lesson 2.6, and it will grow there as Chatterbox learns more commands.
Then read the arms from the top:
"" => {}: an empty message gets empty braces, which means "do nothing". It replaces the emptyifblock from last lesson."/help"printsbot:and theHELPtext."/quit"says goodbye, using your name.othercatches everything else. An arm can be a whole block in braces, and this one counts the message and printsreply(other).replyitself is unchanged: the too-long check and the question check still live there. This is the only arm that adds tomessages_seen, because commands are not messages.
The two jobs are now split cleanly: main decides what kind of line it got, and reply decides what to say to an ordinary message.
Two honest limits. /quit says goodbye but nothing stops yet, because Chatterbox still handles exactly one message: there is nothing to stop. The next lesson adds a loop, and /quit starts meaning "leave". And the message is still written into the program, this time as "/help", so the run under the code shows the help line. The keyboard arrives at the end of the chapter.
Chatterbox v0.3 Hi Sam! I'm Chatterbox. Ask me anything. bot: commands: /help, /quit messages so far: 0
Download this version (zip)Unzip, then cargo run inside the folder.
A stand-alone command table, smaller than Chatterbox and with shorter replies. Finish handle: /help gives the HELP text, /quit gives bye!, an empty message gives (nothing to say), and anything else gives you: plus the text.
A stand-alone command table, smaller than Chatterbox and with shorter replies. Finish handle: /help gives the HELP text, /quit gives bye!, an empty message gives (nothing to say), and anything else gives you: plus the text.
Hidden tests (4): help_lists_commands empty_is_ignored other_text_is_echoed quit_says_bye
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
Say it out loud, then reveal
What does a bare name like other do as a match arm?Reveal
What does "non-exhaustive patterns" (E0004) mean?Reveal
Does "/Help" match the arm "/help"?Reveal
That is lesson 3.2. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
What is match in Rust?
match takes one value and compares it against a list of arms, top to bottom. The first arm whose pattern fits is the one that runs.
What does the underscore mean in a Rust match?
An underscore, _, is a catch-all arm that matches any value without giving it a name. Use it for "everything else" when you do not need the value.
What does error E0004 non-exhaustive patterns mean?
Some possible values have no arm to go to. Add the missing case, or a catch-all arm at the end.
Should I use if or match in Rust?
Use if for a simple yes or no question. Use match when one value can lead to several different actions, such as a list of chat commands.