PluginBench
Skill
Pass
Audit score 90

m14-mental-model

actionbook/rust-skills

How to install m14-mental-model

npx skills add https://github.com/actionbook/rust-skills --skill m14-mental-model
Claude Code
Cursor
Windsurf
Cline
Full instructions (SKILL.md)

Source of truth, from actionbook/rust-skills.


name: m14-mental-model description: "Use when learning Rust concepts. Keywords: mental model, how to think about ownership, understanding borrow checker, visualizing memory layout, analogy, misconception, explaining ownership, why does Rust, help me understand, confused about, learning Rust, explain like I'm, ELI5, intuition for, coming from Java, coming from Python, 心智模型, 如何理解所有权, 学习 Rust, Rust 入门, 为什么 Rust" user-invocable: false

Mental Models

Layer 2: Design Choices

Core Question

What's the right way to think about this Rust concept?

When learning or explaining Rust:

  • What's the correct mental model?
  • What misconceptions should be avoided?
  • What analogies help understanding?

Key Mental Models

ConceptMental ModelAnalogy
OwnershipUnique keyOnly one person has the house key
MoveKey handoverGiving away your key
&TLending for readingLending a book
&mut TExclusive editingOnly you can edit the doc
Lifetime 'aValid scope"Ticket valid until..."
Box<T>Heap pointerRemote control to TV
Rc<T>Shared ownershipMultiple remotes, last turns off
Arc<T>Thread-safe RcRemotes from any room

Coming From Other Languages

FromKey Shift
Java/C#Values are owned, not references by default
C/C++Compiler enforces safety rules
Python/GoNo GC, deterministic destruction
FunctionalMutability is safe via ownership
JavaScriptNo null, use Option instead

Thinking Prompt

When confused about Rust:

  1. What's the ownership model?

    • Who owns this data?
    • How long does it live?
    • Who can access it?
  2. What guarantee is Rust providing?

    • No data races
    • No dangling pointers
    • No use-after-free
  3. What's the compiler telling me?

    • Error = violation of safety rule
    • Solution = work with the rules

Trace Up ↑

To design understanding (Layer 2):

"Why can't I do X in Rust?"
    ↑ Ask: What safety guarantee would be violated?
    ↑ Check: m01-m07 for the rule being enforced
    ↑ Ask: What's the intended design pattern?

Trace Down ↓

To implementation (Layer 1):

"I understand the concept, now how do I implement?"
    ↓ m01-ownership: Ownership patterns
    ↓ m02-resource: Smart pointer choice
    ↓ m07-concurrency: Thread safety

Common Misconceptions

ErrorWrong ModelCorrect Model
E0382 use after moveGC cleans upOwnership = unique key transfer
E0502 borrow conflictMultiple writers OKOnly one writer at a time
E0499 multiple mut borrowsAliased mutationExclusive access for mutation
E0106 missing lifetimeIgnoring scopeReferences have validity scope
E0507 cannot move from &TImplicit cloneReferences don't own data

Deprecated Thinking

DeprecatedBetter
"Rust is like C++"Different ownership model
"Lifetimes are GC"Compile-time validity scope
"Clone solves everything"Restructure ownership
"Fight the borrow checker"Work with the compiler
"unsafe to avoid rules"Understand safe patterns first

Ownership Visualization

Stack                          Heap
+----------------+            +----------------+
| main()         |            |                |
|   s1 ─────────────────────> │ "hello"        |
|                |            |                |
| fn takes(s) {  |            |                |
|   s2 (moved) ─────────────> │ "hello"        |
| }              |            | (s1 invalid)   |
+----------------+            +----------------+

After move: s1 is no longer valid

Reference Visualization

+----------------+
| data: String   |────────────> "hello"
+----------------+
       ↑
       │ &data (immutable borrow)
       │
+------+------+
| reader1    reader2    (multiple OK)
+------+------+

+----------------+
| data: String   |────────────> "hello"
+----------------+
       ↑
       │ &mut data (mutable borrow)
       │
+------+
| writer (only one)
+------+

Learning Path

StageFocusSkills
BeginnerOwnership basicsm01-ownership, m14-mental-model
IntermediateSmart pointers, error handlingm02, m06
AdvancedConcurrency, unsafem07, unsafe-checker
ExpertDesign patternsm09-m15, domain-*

Related Skills

WhenSee
Ownership errorsm01-ownership
Smart pointersm02-resource
Concurrencym07-concurrency
Anti-patternsm15-anti-pattern

Related skills

More from actionbook/rust-skills and the wider catalog.

CO

coding-guidelines

actionbook/rust-skills

Use when asking about Rust code style or best practices. Keywords: naming, formatting, comment, clippy, rustfmt, lint, code style, best practice, P.NAM, G.FMT, code review, naming convention, variable naming, function naming, type naming, 命名规范, 代码风格, 格式化, 最佳实践, 代码审查, 怎么命名

1.2k installsAudited
M0

m07-concurrency

actionbook/rust-skills

CRITICAL: Use for concurrency/async. Triggers: E0277 Send Sync, cannot be sent between threads, thread, spawn, channel, mpsc, Mutex, RwLock, Atomic, async, await, Future, tokio, deadlock, race condition, 并发, 线程, 异步, 死锁

1.2k installsAudited
M1

m10-performance

actionbook/rust-skills

CRITICAL: Use for performance optimization. Triggers: performance, optimization, benchmark, profiling, flamegraph, criterion, slow, fast, allocation, cache, SIMD, make it faster, 性能优化, 基准测试

1.2k installsAudited
M0

m06-error-handling

actionbook/rust-skills

CRITICAL: Use for error handling. Triggers: Result, Option, Error, ?, unwrap, expect, panic, anyhow, thiserror, when to panic vs return Result, custom error, error propagation, 错误处理, Result 用法, 什么时候用 panic

1.2k installsAudited
M0

m01-ownership

actionbook/rust-skills

CRITICAL: Use for ownership/borrow/lifetime issues. Triggers: E0382, E0597, E0506, E0507, E0515, E0716, E0106, value moved, borrowed value does not live long enough, cannot move out of, use of moved value, ownership, borrow, lifetime, 'a, 'static, move, clone, Copy, 所有权, 借用, 生命周期

1.2k installsAudited
M0

m02-resource

actionbook/rust-skills

CRITICAL: Use for smart pointers and resource management. Triggers: Box, Rc, Arc, Weak, RefCell, Cell, smart pointer, heap allocation, reference counting, RAII, Drop, should I use Box or Rc, when to use Arc vs Rc, 智能指针, 引用计数, 堆分配

1.2k installsAudited