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 token_budget. 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: who you are, what the bot is called, and which version it is. Every line that printed a quoted word now uses a name instead.
$ cargo run
Chatterbox v0.2
Hi Sam! I'm Chatterbox.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.
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 by filling the one blank with format! and the names.
Two names are already defined. Make banner() return Chatterbox v0.2 by filling the one blank with format! and the names.
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.
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 token_budget. 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.