Book 1 · Lesson 1.8

Move, clone, and Copy

  • Distinguish move, clone, and Copy types
  • Explain why String moves but i32 copies
  • Choose clone vs reference in handler code

Prerequisites: 1.6

Not every assignment moves heap data.

Trait / typeBehaviorExample
CopyBitwise duplicate on stacki32, bool, [u8; 4]
MoveTransfer ownershipString, Vec<u8>
CloneExplicit deep copypayload.clone()

Use OwnershipVisualizer — toggle Move vs Borrow to see when the original stays valid.

When to clone in services

Clone when two parts of the pipeline need independent copies (e.g. log message + Kafka payload). Prefer borrow when reading without taking ownership.

Cost awareness

Cloning large JSON payloads on every webhook adds CPU — structure types so you move once into Kafka.

Teach-back prompt

Why is let n = counter; counter += 1 valid for i32 but not for String?

Explain String move

Lesson 1.8 check

1. i32 assignment copies because i32 is…