Lesson 1.4 · 6 min
Chatterbox says hello
Time to add the first real piece to Chatterbox: a greeting that uses your name. Small, but it is a complete program, and it is the base every later lesson builds on.
Adds to ChatterboxA greeting that fills in your name.
Chatterbox v0.1
Put the last three lessons together. The program prints a banner, then a greeting that uses a name. Right now the name is typed straight into the code. In chapter 2 you will give it a proper name, and chapter 3 makes Chatterbox read what you type. For now we are only checking that the whole path works: text goes in, the compiler agrees, and a friendly line comes out.
Run it below. Change "Sam" to your own name first; that is a real edit to a real program.
$ cargo run
Chatterbox v0.1
Hi Sam! I'm Chatterbox.The two highlighted lines are what this lesson adds. Read them aloud: the first fills two holes with a name and a version; the second fills two holes with a person and the bot.
Notice the repetition: "Chatterbox" appears twice. If the bot changed its name you would have to change it in two places and hope you found them both. Chapter 2 fixes exactly that.
Make greeting return Hi Sam! I'm Chatterbox. for the name "Sam", and the same sentence with any other name. Fill the blank. &str means a piece of fixed text; chapter 2, lesson 4 explains it (a preview of what is coming).
Make greeting return Hi Sam! I'm Chatterbox. for the name "Sam", and the same sentence with any other name. Fill the blank. &str means a piece of fixed text; chapter 2, lesson 4 explains it (a preview of what is coming).
Hidden tests (3): greets_sam different_names_differ greets_anyone
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
What you have now
Chapter 1 is done. You know what a program is, how to make and run a Rust project, how to print text with holes, how to leave comments, and how to read your first compiler error. Chatterbox greets you by name.
Next, chapter 2: giving values names so Chatterbox does not repeat itself.
Say it out loud, then reveal
What did Chatterbox gain in this lesson?Reveal
Why is typing "Chatterbox" twice a problem?Reveal
That is lesson 1.4. Tick it off so the course remembers where you are.