Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Learn RustChapter 3: Decisions

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.

Three arms, the last one matches anything
fn main() {
let message = "/help";
match message {
"/help" => println!("commands: /help, /quit"),
"/quit" => println!("bye!"),
other => println!("you: {other}"),
}
}
$ cargo run
commands: /help, /quit

The 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:

No arm for "everything else"
fn main() {
let message = "hello";
match message {
"/help" => println!("commands: /help, /quit"),
"/quit" => println!("bye!"),
}
}
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.

PredictWhich arm fires?
fn main() {
let message = "/Help";
match message {
"/help" => println!("help"),
"/quit" => println!("quit"),
other => println!("echo {other}"),
}
}

What does this print?

  • help
  • echo /Help
  • quit
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 empty if block from last lesson.
  • "/help" prints bot: and the HELP text.
  • "/quit" says goodbye, using your name.
  • other catches everything else. An arm can be a whole block in braces, and this one counts the message and prints reply(other). reply itself is unchanged: the too-long check and the question check still live there. This is the only arm that adds to messages_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 after lesson 3.2: the whole program (green lines are new in this lesson)
src/main.rs11 new lines
// Chatterbox: a small chat program, built one lesson at a time.
 
/// What the bot is called. A constant: fixed for the whole program.
const BOT: &str = "Chatterbox";
/// The version as a pair of numbers: (major, minor).
const VERSION: (u32, u32) = (0, 3);
/// The longest message we accept, in bytes.
const MAX_LEN: usize = 200;
/// What /help prints.
const HELP: &str = "commands: /help, /quit";
 
/// The first line Chatterbox prints.
fn banner() -> String {
format!("{} v{}.{}", BOT, VERSION.0, VERSION.1)
}
 
/// The greeting, with a name filled in.
fn greeting(name: &str) -> String {
let mut text = format!("Hi {}! I'm {}.", name, BOT);
text.push_str(" Ask me anything.");
text
}
 
/// What the bot says back to one message.
fn reply(message: &str) -> String {
if message.len() > MAX_LEN {
format!("that is too long (the limit is {} bytes)", MAX_LEN)
} else if message.ends_with('?') {
String::from("Good question! I can only echo for now. Ask me again in chapter 9.")
} else {
format!("you said: {}", message)
}
}
 
fn main() {
let name = "Sam";
let mut messages_seen: u32 = 0;
let quiet = false; // true skips the greeting
 
println!("{}", banner());
if !quiet {
println!("{}", greeting(name));
}
 
// Written in for now. The last lesson of this chapter reads it from the keyboard.
let message = "/help";
 
match message {
"" => {} // nothing typed: nothing to answer
"/help" => println!("bot: {}", HELP),
"/quit" => println!("bot: bye, {}!", name),
other => {
messages_seen += 1;
println!("bot: {}", reply(other));
}
}
println!("messages so far: {}", messages_seen);
}
Chatterbox prints (a real run)
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.

Try itYour turn: the command table

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.

const HELP: &str = "commands: /help, /quit";
 
fn handle(message: &str) -> String {
// use match with one arm per command and a catch-all
String::new()
}
 
fn main() {
println!("{}", handle("/help"));
}

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.

Self-check

Say it out loud, then reveal

Self-checkRetrieval beats re-reading
What does a bare name like other do as a match arm?Reveal
It matches any value, and gives that value the name so you can use it.
What does "non-exhaustive patterns" (E0004) mean?Reveal
Some possible values have no arm. Add a catch-all arm or the missing case.
Does "/Help" match the arm "/help"?Reveal
No. Text patterns must match exactly, capital letters included.

That is lesson 3.2. Tick it off so the course remembers where you are.

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.

Learn Rust by building a chat app · Lesson 3.2 · every compiler message above is real output from rustc 1.99.0 (b940084d7 2026-09-28) (edition 2024) · Vibe Engines · 2026