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 two lessons together. The program prints its 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.
Here is the whole program. The green lines are what this lesson changed.
Chatterbox v0.1 Hi Sam! I'm Chatterbox. Type a message and I will answer.
Download this version (zip)Unzip, then cargo run inside the folder.
Read the two new println! lines aloud. The banner now fills two holes, a name and a version, instead of spelling out Chatterbox v0.1 in one piece. The greeting fills two holes with a person and the bot. The comment between them is a note for you, and the compiler skips it.
This version does not read anything you type yet: Sam comes from the code itself. Download the zip, change "Sam" to your own name, and cargo run it: that is a real edit to a real program. (A preview: at the end of chapter 3, Chatterbox learns to ask your name.)
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, by giving values names.
A greeting needs a goodbye. Make farewell return Bye, Sam! Come back soon. for the name "Sam", and the same sentence with any other name filled in. The name arrives as name. As in the last exercise, the last line with no semicolon is what the function hands back, and right now that line is String::new(), empty text. Replace String::new() with a format!(…) that builds the sentence, and leave the semicolon off.
A greeting needs a goodbye. Make farewell return Bye, Sam! Come back soon. for the name "Sam", and the same sentence with any other name filled in. The name arrives as name. As in the last exercise, the last line with no semicolon is what the function hands back, and right now that line is String::new(), empty text. Replace String::new() with a format!(…) that builds the sentence, and leave the semicolon off.
Hidden tests (3): says_bye_to_sam an_empty_name_still_makes_a_sentence says_bye_to_anyone
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
What you have now
You know what a program is, how to make and run a Rust project, how to print text with holes, and how to leave comments. Chatterbox greets you by name.
Before Chatterbox grows any further, the next lesson spends a few minutes on the compiler's messages: what each part of one means, and how to fix a program by reading them. After that, the last lesson of this chapter turns the banner and the greeting into functions of their own.
Say it out loud, then reveal
What did Chatterbox gain in this lesson?Reveal
Why is typing "Chatterbox" twice a problem?Reveal
What is the difference between println! and format! in the farewell exercise?Reveal
That is lesson 1.4. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
How do I put a variable inside a string in Rust?
Leave a hole {} in the text and pass the value after a comma, as in println!("Hi {}!", name). Each hole is filled in order, left to right.
How do I make a function return text in Rust?
Write -> String after the function name and build the text with format!. The last line of the function, with no semicolon, is the value it hands back.
What is a good first Rust project?
Something small you can grow one step at a time, like the Chatterbox chat program in this course. It starts as a greeting and gains one new idea per lesson.