Book 1 · Lesson 1.6
Ownership rules
- State the three ownership rules
- Demonstrate move vs borrow with a string payload
- Relate moves to Kafka publish handoff
Prerequisites: 1.5
Rust ownership — three rules:
- Each value has one owner
- When owner goes out of scope, value is dropped
- You can borrow (
&T) or move — not both mutably at once
Owner:
ingestion_handlerowner
ingestion_handler owns String::from("webhook payload")stack
value valid while owner in scopeMove in ingestion
When the handler builds a payload and sends it to Kafka, ownership often transfers to the producer buffer — the handler must not use the string afterward unless it cloned.
vs JavaScript
JS objects are shared by reference — garbage collector reclaims when unreachable. Rust catches use-after-move at compile time.
Teach-back prompt
What happens if ingestion moved a buffer to Kafka and then logged it?
Fix borrow errors
Lab complete — nice work.