Lesson 1.7 · 8 min
Chapter review: Hello
Chapter 1 took you from "what is a program?" to a Chatterbox with a banner and a greeting, each in a function of its own. Before the program starts remembering things, check that the whole chapter has stuck: a mixed quiz, then one bigger exercise that uses all of it at once.
Adds to ChatterboxNothing new: a mixed quiz on the whole chapter, and a bigger exercise that uses all of it.
What chapter 1 gave you
Chatterbox v0.1 is small, but it is a real program you made and ran: it prints a banner and greets Sam by name, and both lines come from functions you wrote. Here is the chapter in one line per lesson. If a line feels shaky, the link takes you back.
- 1.1 What a program is: a list of steps the computer follows exactly, in order, starting at
main. The compiler turns your text into a program first; running it comes second. - 1.2 Make and run a project:
cargo newmakes the folder,cargo runbuilds and runs it,cargo checkonly asks "does this make sense?". - 1.3 Printing and comments:
println!fills each{}hole with the next value after the comma,format!builds the same text without printing it, and the compiler skips everything after//. - 1.4 Chatterbox says hello: those pieces put together into the first real version of Chatterbox.
- 1.5 Reading what the compiler says: headline for what, arrow for where, underline for which characters, help line for what to try. E0425 is a name nothing defines; E0308 is a value of the wrong type.
- 1.6 Functions: a named list of steps with inputs (parameters) and an output (the type after the arrow). The last line with no semicolon is the answer it hands back, and a call with the wrong number of inputs is E0061.
Mixed quiz
Seven short programs, all new, and the ideas are shuffled on purpose: a printing question, then an error, then a function. Mixing them is harder than answering seven of the same kind in a row, and that difficulty is what makes it stick. For each one, decide before you pick: does it compile? If it does, what exactly does it print? Every answer below was produced by the real compiler.
What does this print?
Chatterbox v0.1Chatterbox v0.0Chatterbox v0.1 // the banner
Reveal the answer
The program prints: Chatterbox v0.1 The first line inside main starts with //, so the whole line is a comment and the compiler never sees that old banner. On the next line, everything after // is a comment too, so the banner is a note for people, not text to print. Revisit lesson 1.3 if either surprised you.
Does this compile — and if it does, does it run?
- It compiles and runs
- Compile error E0061
- Compile error E0425
Reveal the answer
It does not compile: E0061. banner promises two parameters, a bot and a version, and the call gives it one. A call has to match the function's first line exactly, one argument per parameter, so the compiler stops with E0061 and says which argument is missing. That contract is lesson 1.6.
error[E0061]: this function takes 2 arguments but 1 argument was supplied --> src/main.rs:6:20 | 6 | println!("{}", banner("Chatterbox")); | ^^^^^^-------------- argument #2 of type `&str` is missing | note: function defined here --> src/main.rs:1:4 | 1 | fn banner(bot: &str, version: &str) -> String { | ^^^^^^ ------------- help: provide the argument | 6 | println!("{}", banner("Chatterbox", /* &str */)); | ++++++++++++
What does this print?
Sam sent 3 messages and 12 wordsSam sent 1 + 2 messages and 3 * 4 words{} sent {} messages and {} words
Reveal the answer
The program prints: Sam sent 3 messages and 12 words The holes are filled left to right with the values after the comma. 1 + 2 and 3 * 4 are not text, they are sums, so Rust works each one out first and prints the result. Only what is inside the quotes is printed as it is written (lesson 1.3).
Does this compile — and if it does, does it run?
- It compiles and runs
- Compile error E0425
- Compile error E0061
Reveal the answer
It does not compile: E0425. The parameter is called name, but the body asks for nmae. The compiler matches names letter by letter and nothing in the program is called nmae, so it stops with E0425, "cannot find value". The help line even suggests the parameter it probably meant. Lesson 1.5 takes this message apart.
error[E0425]: cannot find value `nmae` in this scope --> src/main.rs:2:39 | 2 | format!("Hi {}! I'm Chatterbox.", nmae) | ^^^^ | help: a local variable with a similar name exists | 2 - format!("Hi {}! I'm Chatterbox.", nmae) 2 + format!("Hi {}! I'm Chatterbox.", name) |
footer is written first in the file. What is the first line this program prints?
2 messages todayChatterbox v0.1footer
Reveal the answer
The program prints: Chatterbox v0.1 Defining a function only writes the recipe down; nothing runs until it is called. The program starts in main, and main calls banner() first, so the banner is the first line, wherever the functions sit in the file. Lessons 1.1 and 1.6 both lean on this.
Does this compile — and if it does, does it run?
- It compiles and runs
- Compile error E0308
- Compile error E0061
Reveal the answer
It does not compile: E0308. The last line has no semicolon, so its value is what banner hands back. But println! puts text on the screen and produces nothing, written (), while the arrow promised a String. Expected String, found (): E0308. Building the text with format! is what fits that promise (lessons 1.5 and 1.6).
error[E0308]: mismatched types --> src/main.rs:2:5 | 2 | println!("Chatterbox v0.1") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `()`
Does this compile — and if it does, does it run?
- It compiles and runs
- Compile error E0425
- Compile error E0061
Reveal the answer
It compiles and runs. It compiles and prints Chatterbox v0.1 | Hi Ada!. The compiler reads the whole file before it complains about a name, so functions written below main are found. And a call like banner() is an expression, so it can fill a hole inside another function's format!. Revisit lesson 1.6 if you expected E0425.
Milestone: the opening line
Now everything at once. Chatterbox's opening becomes one line built from three functions: banner makes the name and version, greeting says hi to one person, and opening joins the two. Read each function's first line and the comment above it carefully: together they are the whole specification. The hidden tests try more than one bot name and more than one person, so nothing can be typed in where a parameter belongs.
Write the three bodies. banner("Chatterbox", "0.1") gives Chatterbox v0.1. greeting("Sam", "Chatterbox") gives Hi Sam! I'm Chatterbox.. opening("Sam") gives Chatterbox's banner at version 0.1, then | , then Chatterbox's greeting to Sam: Chatterbox v0.1 | Hi Sam! I'm Chatterbox.. Each todo!() is a placeholder that compiles but stops the program if it is reached; replace it.
Write the three bodies. banner("Chatterbox", "0.1") gives Chatterbox v0.1. greeting("Sam", "Chatterbox") gives Hi Sam! I'm Chatterbox.. opening("Sam") gives Chatterbox's banner at version 0.1, then | , then Chatterbox's greeting to Sam: Chatterbox v0.1 | Hi Sam! I'm Chatterbox.. Each todo!() is a placeholder that compiles but stops the program if it is reached; replace it.
Hidden tests (7): banner_for_any_bot banner_for_chatterbox greeting_for_sam greeting_uses_both_inputs opening_for_anyone opening_with_an_empty_name opening_for_sam
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
Where this leaves you
If the quiz went well and the tests are green, you have the whole of chapter 1: you can read a short program and say what it will do, read the compiler's message when it will not, and split a job into functions that hand answers to each other. Notice how often the exercise typed "Chatterbox": four times, in two different functions. Chapter 2 starts by giving values names, so a fact like that is written once.
Say it out loud, then reveal
Say, in order, the four places you look when the compiler prints an error.Reveal
You see E0425, E0308 and E0061. What does each one say in plain words?Reveal
What is the difference between <code>println!</code> and <code>format!</code>, and which one belongs on the last line of a function that returns a <code>String</code>?Reveal
println! prints and produces nothing; format! builds the same text and hands it back. The last line of a function that returns a String needs format!, with no semicolon.Why can <code>opening</code> use <code>banner(…)</code> inside its own <code>format!</code>?Reveal
Your Chatterbox so far: v0.1 (after lesson 1.6)
Download this version (zip)Unzip, then cargo run inside the folder.
That is lesson 1.7. Tick it off so the course remembers where you are. That is also the end of chapter 1: a few minutes with the course flashcards now makes it stick, and the chapter’s cheat sheet has every idea on one screen.
Go deeper
Questions people ask
What should I know after the first chapter of Rust?
How to make and run a project with cargo, how to print text with holes and leave comments, how to read a compiler error and fix E0425, E0308 and E0061, and how to write functions with parameters and a return value.
Why does this course mix quiz questions instead of grouping them by topic?
Answering a mix forces you to decide which idea a question is about before you answer it, which is the skill you need when reading real code. It feels harder than a block of the same kind, and it is remembered better.
I got some quiz answers wrong. Should I go on to chapter 2?
Read the explanation under each wrong answer and revisit the lesson it names. If the milestone exercise passes its tests, you are ready; chapter 2 uses functions and format! on every page.
Can a Rust function call another function?
Yes. A call is an expression that produces a value, so it can appear anywhere a value can, including inside another function and inside a format! hole. The order the functions are written in the file does not matter.
