Lesson 3.5 · 11 min
Reading a line
Chatterbox has been talking to a canned list. This lesson plugs in the keyboard: it reads a line you type, answers it, and goes back for the next, until you type /quit.
Adds to ChatterboxChatterbox reads what you type.
Where input comes from
The line use std::io; at the top brings Rust's input and output tools into reach, so you can write io::stdin() instead of the full path.
A program has an input stream, called standard input or stdin. When you run a program in a terminal, whatever you type goes there. To pull one line out of it you call read_line.
The in-browser runner on this page has no keyboard to read, so this lesson works in two halves. First the real program, which you run in your own terminal. Then the same thinking as a function over text, which you can test here.
Four new pieces, and you only need the idea of each for now.
String::new()makes an empty piece of text that can grow. It islet mutbecauseread_lineis going to write into it.&mut linemeans "here, you may change this". The&mutis lending the text out to be changed. Chapter 4 explains lending in full; for now, read it as "write your answer here"..unwrap()means "if reading failed, stop the program". Chapter 7 replaces it with something kinder..trim()removes whitespace at both ends.read_linekeeps the Enter key you pressed as a newline at the end of the text, so withouttrima typed/quitwould arrive as/quitplus a newline, and would match no arm.
That last point is worth checking by eye. Here is a real run with the input piped in:
$ echo "hello there" | cargo run -q
you typed: hello thereReading until there is nothing left
read_line also returns how many bytes it read. When the input has ended (you pressed Ctrl-D, or a pipe ran dry) that number is 0. A chat loop has to notice, or it spins forever on empty reads. So the loop has two ways out: the user types /quit, or the input ends.
Before the loop, one more important piece: line.clear() at the top of each turn. read_line adds to what is there, it does not replace it.
That is the whole program, and it is Chatterbox v0.3: it loops, reads, decides with match, and answers with a function. A session looks like this:
$ printf 'hi\n/help\n\nhow are you\n/quit\n' | cargo run -q
Chatterbox v0.3. Type /help or /quit.
you: hi
commands: /help, /quit
(nothing to say)
you: how are you
bye!
messages so far: 4Finish run_chat. For each line of input: trim it, stop at /quit (adding bye! first), otherwise add reply(line). Every output line ends with a newline.
Finish run_chat. For each line of input: trim it, stop at /quit (adding bye! first), otherwise add reply(line). Every output line ends with a newline.
Hidden tests (4): echoes_and_helps no_input_no_output quit_stops_the_chat whitespace_is_trimmed
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
What you have now
Chapter 3 is done and Chatterbox is a real, if shy, chat program. It reacts to what you type, it knows two commands, and its logic is in a function you can test. Everything it says is text it holds for a moment and forgets. In chapter 4 we look at the idea Rust is built around, ownership: who is responsible for a piece of text while it moves between main and reply.
Say it out loud, then reveal
Why call trim() on what read_line gave you?Reveal
What does read_line returning 0 mean?Reveal
Why does the exercise take a &str of lines instead of reading the keyboard?Reveal
That is lesson 3.5. Tick it off so the course remembers where you are.