Lesson 5.1 · 7 min
Bundling fields: struct
Chatterbox keeps passing around an author in one string and the text in another, and hoping they stay together. A struct gives them one name and one owner.
Adds to ChatterboxA Message struct.
Two strings that belong together
Chatterbox v0.4 records every line into a transcript. But a line has two facts: who said it, and what they said. Today those travel as two loose strings, and nothing stops you from handing the bot's text to the wrong author.
A struct is a named bundle of fields (named slots, each holding one value). You decide the fields once, and from then on a Message is one thing you can pass around, store and borrow.
$ cargo run
you: helloThe first four lines describe the shape: a Message has an author and a text, both String (lesson 2.5). Inside main we build one by naming every field. Then m.author and m.text read a field with a dot.
Every field must be given. Leave one out and the compiler stops you, which is the point: a Message with no author cannot exist.
error[E0063]: missing field `text` in initializer of `Message` --> src/main.rs:7:13 | 7 | let m = Message { | ^^^^^^^ missing `text`
E0063 says "missing field text". Not a warning, not a default of empty: the program does not build until the shape is complete.
A struct owns its fields
Remember the one-key model from chapter 4. Each String has exactly one owner. Inside a Message, the message owns its author and its text. When the message goes away, its fields go with it. Move the message and you move everything inside it in one go.
That is why the new record in Chatterbox takes &Message: it only needs to look, so it borrows. The caller keeps the key.
What does this print?
bot: pingping: botit does not compile
Reveal the answer
The program prints: bot: ping Fields are matched by name, not by position. Writing text first does not swap them. That is a quiet gift of structs: you cannot mix up two strings of the same type.
Chatterbox v0.5: every line is a Message
Here is the whole program after this lesson. The green lines are the change; everything else is exactly what you had at the end of chapter 4.
- At the top, the
Messagestruct: anauthorand atext, both ownedStrings. recordused to take two loose strings,whoandtext. Now it takes one borrowed&Messageand readsmsg.authorandmsg.textwith a dot. The&inpush_str(&msg.author)lends the field topush_str, which only needs to read it; the message keeps its own text. It still gets&muton the transcript, because it adds to it (lesson 4.5).- In the loop, what you typed becomes
said, aMessagefromyou, and the bot's answer becomesanswer, aMessagefrombot. Both are recorded, andanswer.textis printed.
Type /history and the transcript comes out exactly as before. That is the point of this change: nothing you can see is different, but the author and the text can no longer be handed to record in the wrong order.
Sam hello how are you? /history /quit
Chatterbox v0.5 What should I call you? Hi Sam! I'm Chatterbox. Ask me anything. bot: you said: hello bot: Good question! I can only echo for now. Ask me again in chapter 9. you: hello bot: you said: hello you: how are you? bot: Good question! I can only echo for now. Ask me again in chapter 9. bot: bye, Sam! messages so far: 2
Show the whole program (open it after you have tried the exercise below: it contains the answer)
Download this version (zip)Unzip, then cargo run inside the folder.
One line deserves a closer look: last = said.text;. You can move a single field out of a struct. The text String is handed over to last (for /again, from lesson 4.3), and the rest of said is not used again, so the compiler is happy. If a later line tried to use said.text, you would get the same "use of moved value" error you met in chapter 4.
This is the whole Chatterbox program with the body of record taken out. Put it back: add the author, a colon and a space, the text, and a line break to the end of the transcript. The hidden tests compare the exact text the transcript ends up with.
This is the whole Chatterbox program with the body of record taken out. Put it back: add the author, a colon and a space, the text, and a line break to the end of the transcript. The hidden tests compare the exact text the transcript ends up with.
Hidden tests (4): lines_pile_up_in_order empty_text_still_gets_its_line the_message_is_only_borrowed one_message_is_one_line
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
Say it out loud, then reveal
What does a struct give you that two loose strings do not?Reveal
Who owns the <code>text</code> inside a <code>Message</code>?Reveal
Why does <code>record</code> take <code>&Message</code> instead of <code>Message</code>?Reveal
That is lesson 5.1. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
What is a struct in Rust?
A struct is a type you define that bundles several named values, called fields, into one thing. For example, a Message struct can hold an author and a text together.
How do I read a field of a struct in Rust?
Write the value, a dot, and the field name, such as m.author. If you only have a reference to the struct, the same dot works.
What does error E0063 missing field mean?
You built a struct value without giving every field a value. Rust has no automatic empty default, so add the missing field.
Does the order of fields matter when building a struct?
No. Fields are matched by name, so you can list them in any order and they still land in the right place.
