Lesson 1.3 · 7 min
Printing and comments
Chatterbox is a program that talks, so the first thing to learn is how a Rust program says things. You will fill holes in text, leave notes for yourself, and meet the compiler telling you off for the first time.
Adds to ChatterboxChatterbox prints a banner.
Compiler errors you will meet E0425
Holes in the text
You can print fixed text, but a chat program mostly prints text that depends on something. println! lets you leave a hole {} in the text and give it the value to put there, after a comma.
$ cargo run
Chatterbox says hello to youWhat does this print?
2 + 3 = 55{} + {} = {}
Reveal the answer
The program prints: 2 + 3 = 5 The holes are filled left to right: the first {} gets 2, the second gets 3, the third gets the result of 2 + 3. Numbers print just like text.
Notes for yourself
Anything after // on a line is a comment. The compiler skips it completely. Comments are for the next person who reads the code, and that person is usually you in a month.
A comment can say two kinds of thing. It can say what a line does, which helps while the code is still new to you. Or it can say why the line is there, which the code itself can never tell you. While you are learning, both kinds are welcome; as the code becomes familiar, the "why" comments are the ones worth keeping.
$ cargo run
Chatterbox v0.1Text you keep instead of printing
Sometimes you want the finished text as a value, not on the screen yet. format! works exactly like println!, but hands you the text back instead of printing it. You will use it in the exercise below.
The compiler has opinions
Now the useful part. What happens if you use something the program has never heard of?
error[E0425]: cannot find value `user` in this scope --> src/main.rs:2:28 | 2 | println!("Hello, {}!", user); | ^^^^ not found in this scope
Read the error from the top. "cannot find value user in this scope" means: you used the word user, and nothing in the program has given that word a meaning. (A scope is the part of the program where a name exists. Later in chapter 2 you will see exactly where a name stops being visible, and why.) The arrows in the message point at the exact spot. The code in brackets, E0425, is the error's ID. You can look any ID up, and this course keeps a list of them.
The fix is to give user a value first. That is the next chapter. For now just notice: the program did not run at all. The compiler caught it before anything printed. Two lessons from now you learn to read every part of a message like this one, because you will see a lot of them, and each one is a mistake that never reaches a user.
Chatterbox prints a banner
Back to the project. Cargo's placeholder Hello, world! is gone. In its place Chatterbox opens with a banner, a first line that says who it is and which version is running, then one line telling you what it is for. The comment above the banner says what it is: a reminder for you while the program is young. The green lines are the ones this lesson changed.
It cannot keep that promise yet: reading what you type comes in chapter 3. For now the banner is the whole show.
Chatterbox v0.1 Type a message and I will answer.
Download this version (zip)Unzip, then cargo run inside the folder.
Chatterbox will one day print a summary of the chat. Make summary() return 3 sent, 4 received, 7 in total, and let Rust work out the 7 from 3 + 4 instead of typing it. The box called You'll meet these early at the top of the page explains the two words you have not met yet. Inside a function, the last line with no semicolon at its end is the answer the function hands back. Right now that line is String::new(), an empty piece of text, so replace String::new() with your format!(…), and leave the semicolon off.
Chatterbox will one day print a summary of the chat. Make summary() return 3 sent, 4 received, 7 in total, and let Rust work out the 7 from 3 + 4 instead of typing it. The box called You'll meet these early at the top of the page explains the two words you have not met yet. Inside a function, the last line with no semicolon at its end is the answer the function hands back. Right now that line is String::new(), an empty piece of text, so replace String::new() with your format!(…), and leave the semicolon off.
Hidden tests (3): nothing_extra_at_the_ends the_whole_line the_total_is_worked_out
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
Say it out loud, then reveal
What does {} do inside println!?Reveal
What does the compiler do with a // comment?Reveal
What does "cannot find value in this scope" tell you?Reveal
That is lesson 1.3. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
How do I write a comment in Rust?
Put two slashes, //, and everything after them on that line is a comment. The compiler ignores it; it is a note for people reading the code.
What is the difference between println! and format! in Rust?
println! prints the finished text to the screen. format! builds exactly the same text but hands it back to you as a value instead of printing it.
What does error E0425 mean in Rust?
E0425 means the compiler cannot find a name you used. Usually the name was never given a value, or it is spelled differently from where it was made.
Why does println! have an exclamation mark?
The ! marks it as a macro, a special kind of command that Rust expands into more code before compiling. For now you can treat it as a normal step that prints.
Sources Rust by Example: formatted print