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?".
$ 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:
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.
What does this print?
chattyquiettrue
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.
$ cargo run
Chatterbox v0.3
Hi Sam! I'm Chatterbox.
(nothing to say)
messages so far: 0Finish 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.
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.
Say it out loud, then reveal
What kind of value must an if condition be?Reveal
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
What does it mean that if is an expression?Reveal
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.
Sources The Rust Book: control flow