Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

ownership-rules

Ownership rules

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Ownership rules: the syntax, APIs, and habits you need before advancing in Rust.

Ownership is Rust’s defining idea—interviewers ask you to explain moves, borrows, and why the compiler rejects dangling pointers.

You will apply Ownership rules in contexts like: Infrastructure CLIs, proxies, game engines, blockchain nodes, and latency-sensitive backends.

Write Rust with fn main(), click Run on server—the dev runner compiles main.rs with rustc and runs the binary; fix borrow errors from stderr (requires Rust toolchain; LEARNING_RUNNER_ENABLED=true). Also read rustc errors literally—they point at the exact borrow or move violation.

When you can explain the previous lesson's ideas without copying starter code.

Ownership is Rust's contract for memory safety. Every value has exactly one owner; when the owner goes out of scope, Rust calls drop automatically.

The three rules

  1. Each value has one owner at a time
  2. Only one owner—when the owner scope ends, the value is dropped
  3. While borrowed, the owner cannot move or mutate in conflicting ways

Scope and drop

{ let s = String::from("hi"); } // s dropped here

Important interview questions and answers

  1. Q: Who frees heap memory for a String?
    A: Rust when the owning variable goes out of scope—no manual free in safe code.
  2. Q: Can two variables own the same String?
    A: Not simultaneously—ownership moves or is shared via borrowing/reference counting patterns.

Self-check

  1. When is memory for a heap value freed?
  2. How many owners at once?

Pitfall: Using a variable after it was moved causes compile errors—compare with Java where references are copied freely.

Interview prep

What problem does ownership solve?

Prevents double-free, use-after-free, and data races in safe code without runtime GC overhead.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Three rules list?
  • Move vs copy?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump