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:

  1. Each value has one owner
  2. When owner goes out of scope, value is dropped
  3. You can borrow (&T) or move — not both mutably at once
Owner: ingestion_handler
owneringestion_handler owns String::from("webhook payload")
stackvalue valid while owner in scope

Move 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

Lesson 1.6 check

1. After a move, the original owner…