Vibe Engines
YouTube

Lesson 3.1 · 8 min

Choosing with if

So far Chatterbox does the same thing every time. A chat program has to react: say one thing when you type something, and another when you type nothing. That choice is an if.

Adds to ChatterboxEmpty input is ignored.

Two roads

An if looks at a question that has a yes or no answer, and runs one block of code for yes. An optional else block runs for no. Exactly one of the two blocks runs, never both.

The question is any piece of code that comes out as true or false: the booleans from lesson 2.3. A text value has a handy one built in, is_empty(), which answers "is there nothing in here?".

One road is taken
fn main() {
let message = "";
if message.is_empty() {
println!("(nothing to say)");
} else {
println!("you: {message}");
}
}
$ cargo run
(nothing to say)

Change message to "hi" and run it again. The other road is taken. Notice there are no brackets around the question, and the curly braces after it are required.

The question must be a bool

Some languages let you write if 1 or if "text" and quietly decide what counts as true. Rust does not guess. The condition must really be a bool, or it will not compile:

A number is not a yes or no
fn main() {
let message_count = 3;
if message_count {
println!("we have messages");
}
}
error[E0308]: mismatched types
 --> src/main.rs:3:8
  |
3 |     if message_count {
  |        ^^^^^^^^^^^^^ expected `bool`, found integer

"mismatched types" with E0308 is the compiler saying: I wanted a bool here and you gave me a number. The fix is to ask a real question, such as message_count > 0. Comparisons like >, < and == always give back a bool.

Predictif gives back a value
fn main() {
let message_count = 7;
let label = if message_count > 5 { "chatty" } else { "quiet" };
println!("{label}");
}

What does this print?

  • chatty
  • quiet
  • true
Reveal the answer

The program prints: chatty In Rust if is not only a command, it is a value. 7 is greater than 5, so the first block is chosen and its last line, "chatty", becomes the value that label holds. Both blocks must give the same kind of value, because label can only be one kind.

Chatterbox v0.3, first piece

Here is the whole program so far. The highlighted lines are new: Chatterbox now holds a message, ignores it when it is empty, and counts it when it is not. Set message to something and watch the count change.

src/main.rs: Chatterbox v0.3
fn main() {
let name = "Sam";
let bot = "Chatterbox";
let version = "0.3";
let mut message_count = 0;
 
println!("{bot} v{version}");
println!("Hi {name}! I'm {bot}.");
 
let message = "";
if message.is_empty() {
println!("(nothing to say)");
} else {
message_count += 1;
println!("you: {message}");
}
println!("messages so far: {message_count}");
}
$ cargo run
Chatterbox v0.3
Hi Sam! I'm Chatterbox.
(nothing to say)
messages so far: 0
Try itYour turn: answer or stay quiet

Finish respond. For an empty message it returns "(nothing to say)". For anything else it returns you: followed by the message. You don't need to understand fn yet (chapter 3, lesson 4 explains it): just fill in the body.

Finish respond. For an empty message it returns "(nothing to say)". For anything else it returns you: followed by the message. You don't need to understand fn yet (chapter 3, lesson 4 explains it): just fill in the body.

fn respond(message: &str) -> String {
// empty -> "(nothing to say)", otherwise "you: <message>"
String::new()
}
 
fn main() {
println!("{}", respond("hello"));
}

Hidden tests (3): empty_is_ignored text_is_echoed a_single_space_is_still_text

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 kind of value must an if condition be?Reveal
A bool: true or false. Rust will not turn numbers or text into one for you.
Can both the if block and the else block run?Reveal
No. Exactly one of them runs.
What does it mean that if is an expression?Reveal
It produces a value, so you can write let x = if … { a } else { b };. Both blocks must give the same kind of value.

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

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