Lesson 3.3 · 10 min
Doing things again
A chat is not one message, it is a stream of them. To handle the next message the same way as the last one you do not copy the code, you tell Rust to repeat it. Rust has three loops, and you will use all three.
Adds to ChatterboxThe chat loop.
Three loops
loop repeats forever, until you say break. while repeats as long as a question stays true. for goes through a collection (a group of values, such as a list) one item at a time and stops by itself at the end.
Start with while, which reads like English:
$ cargo run
retries left: 3
retries left: 2
retries left: 1
giving upThe value changes inside the loop (retries -= 1, which is why it is let mut from lesson 2.2). If nothing ever made the question false the loop would never end, so check that something does.
Now loop with break. It is the right tool when the exit is in the middle of the work:
$ cargo run
stopped after 4 turnsfor: one item at a time
Most of the time you have a list and want to do something to each item. That is for. A list of fixed size is written in square brackets (Rust calls this an array), and for x in list gives each item the name x in turn. (Lists that grow are chapter 6; this fixed one is all we need now.)
What does this print?
hihi /quit hellohello
Reveal the answer
The program prints: hi The loop prints hi, then sees /quit and leaves the whole loop with break. The third item is never looked at.
break has a smaller sibling, continue. Where break leaves the whole loop, continue skips only the rest of this turn and jumps straight to the next item. It is the tool for "ignore this one and carry on", such as an empty message:
$ cargo run
you: hi
you: helloThe empty message never reaches the println!, but unlike break, the loop goes on and hello is still printed. Both words work the same way inside loop and while too.
If you have coded before
for message in messages:, break and continue mean the same thing. while too. The one difference is loop, which Python writes as while True:.
Chatterbox v0.3: the chat loop
A chat is many messages, so the single written-in message becomes a pretend conversation: an array of six. The match from last lesson moves, unchanged, inside a for, so every message gets the same treatment, and reply and HELP do not change at all. The keyboard is still missing (it arrives at the end of this chapter), but Chatterbox now works through this list exactly the way it will soon work through yours.
The one new idea inside the loop is in the /quit arm. It still says goodbye, and then break leaves the loop, so /quit finally means "leave". Follow the list through the run under the code:
"hello"is echoed and counted.""is skipped without a word."/help"lists the commands."are you awake?"gets the question answer and is counted."/quit"says goodbye and breaks out."never answered"is never looked at, which is why it is called that.
So the count at the end is 2. This version reads its own list instead of the keyboard.
Chatterbox v0.3 Hi Sam! I'm Chatterbox. Ask me anything. bot: you said: hello bot: commands: /help, /quit bot: Good question! I can only echo for now. Ask me again in chapter 9. bot: bye, Sam! messages so far: 2
Download this version (zip)Unzip, then cargo run inside the folder.
A small warm-up, separate from Chatterbox. Finish count_before_quit. It receives four messages and returns how many come before the first /quit. If there is no /quit, that is all four. The type [&str; 4] means an array of exactly 4 pieces of text.
A small warm-up, separate from Chatterbox. Finish count_before_quit. It receives four messages and returns how many come before the first /quit. If there is no /quit, that is all four. The type [&str; 4] means an array of exactly 4 pieces of text.
Hidden tests (3): no_quit_counts_all quit_first_counts_none stops_at_quit
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
Say it out loud, then reveal
Which loop repeats until you call break?Reveal
loop.When is for a better choice than while?Reveal
What does break do inside a for loop?Reveal
What is the difference between break and continue?Reveal
break leaves the whole loop. continue skips only the rest of the current turn and goes on with the next item.That is lesson 3.3. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
What are the three kinds of loop in Rust?
loop repeats until you break out of it, while repeats as long as a condition stays true, and for visits each item of a collection once and then stops.
How do I stop a loop early in Rust?
Write break. The loop ends at once and the program carries on with the line after it. To skip just one turn and keep looping, write continue instead.
What is an array in Rust?
An array is a list with a fixed number of items, all of the same type, written in square brackets such as ["hi", "bye"]. Lists that can grow are called Vec.
Why does my while loop never end?
Nothing inside it ever makes the condition false. Make sure the loop changes the value it is checking, for example by counting down.