Lesson 2.1 · 7 min
Names for values
Last chapter, "Chatterbox" was typed into the code twice. Change the bot's name and you have to hunt down every copy. A name for a value fixes that: say it once, use it anywhere.
Adds to ChatterboxThe bot gets a name and a version.
Say it once
A program mostly moves values around: a name, a number, a line of text. If you want to use the same value in three places, typing it three times is a bug waiting to happen. Instead you give it a name and use the name.
In Rust you do that with let. Read let bot = "Chatterbox"; as: "let bot be the text Chatterbox". From that line on, writing bot means that text.
$ cargo run
I am Chatterbox.
Call me Chatterbox.Three small rules go with it. A let line ends with a semicolon. A name is written in lowercase words joined by underscores, like messages_seen. And a name must be created before the line that uses it, which is exactly the lesson of error E0425 from chapter 1.
Names can also sit right inside the quotes. println!("I am {bot}.") does the same thing as passing bot after a comma. Use whichever reads better; this course mostly uses the comma form so the values are easy to see.
What does this print?
Ada meets SamSam meets Adasecond meets first
Reveal the answer
The program prints: Ada meets Sam The name is replaced by its value, and the holes are filled in the order you list them. second comes first in the list, so Ada goes in the first hole. The word second itself is never printed.
Chatterbox v0.2, step one
Here is Chatterbox with its repeated text turned into three names in main: who you are (name), what the bot is called (bot), and which version it is (version). The version is 0.2 now, because a new chapter means a new version.
There is one catch, and it shapes the green lines. A name made with let inside main exists only inside main. banner and greeting cannot see it. So the two functions from lesson 1.6 each gain parameters: banner(bot: &str, version: &str) and greeting(name: &str, bot: &str), and their format! lines fill the holes from those parameters instead of from quoted words. main hands its names over at the call: banner(bot, version) and greeting(name, bot). It is the "change the contract" fix from the E0061 coach, used for real.
name is still written into the code. A preview: at the end of chapter 3, Chatterbox asks for it.
Chatterbox v0.2 Hi Sam! I'm Chatterbox. Type a message and I will answer.
Download this version (zip)Unzip, then cargo run inside the folder.
Rename the bot now and you change one line. That is the whole point of a name: one place says what the value is, every other place just points at it. (Passing bot into every function that needs it will get tiresome as Chatterbox grows. The last lesson of this chapter has a better home for facts that never change.)
One more thing, and it matters for the next lesson. A name made with let cannot be pointed at a different value later. It is fixed for good. This is on purpose: when you read a name three screens down, you know it still means what it meant at the top. As a preview, how to allow a change, and why you usually should not, comes in the next lesson.
Two names are already defined. Make banner() return Chatterbox v0.2. The last line of the body, the one with no semicolon, is what the function hands back (lesson 1.6), and right now it is String::new(), empty text. Replace String::new() with a format!(…) that uses the two names, and leave the semicolon off.
Two names are already defined. Make banner() return Chatterbox v0.2. The last line of the body, the one with no semicolon, is what the function hands back (lesson 1.6), and right now it is String::new(), empty text. Replace String::new() with a format!(…) that uses the two names, and leave the semicolon off.
Hidden tests (3): full_banner mentions_version name_comes_first
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
What you have now
Chatterbox has a name, a bot and a version, each written once. Next it needs to count something that really does change as the chat goes on.
Say it out loud, then reveal
What does let bot = "Chatterbox"; do?Reveal
Why is a name better than typing the text in two places?Reveal
Can a name made with plain let be pointed at a different value later?Reveal
That is lesson 2.1. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
What is a variable in Rust?
A name for a value, made with let. Rust calls it a binding: the name is bound to the value.
How do I name things in Rust?
Lowercase words joined by underscores, such as messages_seen. Only types and a few other things start with a capital.
Can I use a name before the let that makes it?
No. The name must be created on an earlier line, or the compiler reports it cannot find the value.
