Vibe Engines
YouTube

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 one item at a time and stops by itself at the end.

Start with while, which reads like English:

while: keep going while the question is true
fn main() {
let mut retries = 3;
while retries > 0 {
println!("retries left: {retries}");
retries -= 1;
}
println!("giving up");
}
$ cargo run
retries left: 3
retries left: 2
retries left: 1
giving up

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

loop: forever, until break
fn main() {
let mut turns = 0;
loop {
turns += 1;
if turns == 4 {
break;
}
}
println!("stopped after {turns} turns");
}
$ cargo run
stopped after 4 turns

for: 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, 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.)

PredictWhere does it stop?
fn main() {
let messages = ["hi", "/quit", "hello"];
for message in messages {
if message == "/quit" {
break;
}
println!("{message}");
}
}

What does this print?

  • hi
  • hi /quit hello
  • hello
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.

Chatterbox v0.3, the chat loop

There is no keyboard yet (two lessons from now), so Chatterbox works through a canned list of messages the way it will soon work through yours. The match from last lesson moves inside a for, and /quit now does what it says: it prints a goodbye and leaves the loop. Everything after /quit is never read.

src/main.rs: Chatterbox v0.3
fn main() {
let name = "Sam";
let bot = "Chatterbox";
let version = "0.3";
let mut message_count = 0;
let messages = ["hello", "", "/help", "how are you", "/quit", "never seen"];
 
println!("{bot} v{version}");
println!("Hi {name}! I'm {bot}.");
 
for message in messages {
match message {
"" => println!("(nothing to say)"),
"/help" => println!("commands: /help, /quit"),
"/quit" => {
println!("bye, {name}!");
break;
}
other => {
message_count += 1;
println!("you: {other}");
}
}
}
println!("messages so far: {message_count}");
}
$ cargo run
Chatterbox v0.3
Hi Sam! I'm Chatterbox.
you: hello
(nothing to say)
commands: /help, /quit
you: how are you
bye, Sam!
messages so far: 2
Try itYour turn: how many before /quit?

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. You don't need to understand fn yet (chapter 3, lesson 4 explains it): just fill in the body. The type [&str; 4] means an array of exactly 4 pieces of fixed text.

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. You don't need to understand fn yet (chapter 3, lesson 4 explains it): just fill in the body. The type [&str; 4] means an array of exactly 4 pieces of fixed text.

fn count_before_quit(messages: [&str; 4]) -> u32 {
let mut count = 0;
// walk the messages; stop at "/quit"
count
}
 
fn main() {
println!("{}", count_before_quit(["a", "b", "/quit", "c"]));
}

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.

Self-check

Say it out loud, then reveal

Self-checkRetrieval beats re-reading
Which loop repeats until you call break?Reveal
loop.
When is for a better choice than while?Reveal
When you have a collection and want to visit each item once. It stops by itself at the end.
What does break do inside a for loop?Reveal
It leaves the loop at once. The rest of the items are never visited.

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

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