Ownership and Borrowing
所有权与借用
What you’ll learn: The core Rust model that replaces GC-managed shared references with explicit ownership, borrowing, and moves.
本章将学习: Rust 最核心的模型,也就是如何用显式的所有权、借用和 move,替代 GC 托管下那种共享引用世界。Difficulty: 🟡 Intermediate
难度: 🟡 中级
This chapter is the real dividing line between “Rust syntax” and “Rust thinking.”
这一章才是真正把“Rust 语法”跟“Rust 思维”分开的分水岭。
Ownership in One Sentence
一句话理解所有权
Every value has an owner, and when that owner goes out of scope, the value is dropped.
每个值都有一个 owner,当 owner 离开作用域时,这个值就会被释放。
Moves
move
#![allow(unused)]
fn main() {
let a = String::from("hello");
let b = a;
// a is no longer usable here
}
For Java developers, this is the first major shock. Assignment is not always “another reference to the same object.” Sometimes it is ownership transfer.
对 Java 开发者来说,这通常是第一次大的冲击。赋值不再总是“多了一个指向同一对象的引用”,它有时真的是所有权转移。
Borrowing
借用
#![allow(unused)]
fn main() {
fn print_name(name: &str) {
println!("{name}");
}
}
Borrowing lets code read a value without taking ownership.
借用允许代码读取一个值,而不用把所有权拿走。
Mutable Borrowing
可变借用
#![allow(unused)]
fn main() {
fn append_world(text: &mut String) {
text.push_str(" world");
}
}
Rust allows mutation through a borrowed path, but only under rules that prevent conflicting access.
Rust 允许通过借用路径修改数据,但前提是必须遵守那套防止冲突访问的规则。
The Important Rule
最关键的规则
At a given moment, you may have:
在同一时刻,只能二选一:
- many immutable references
很多个不可变引用。 - or one mutable reference
或者一个可变引用。
That rule prevents a large class of race conditions and aliasing bugs.
这条规则会拦掉大量竞争条件和别名相关 bug。
Why Java Developers Struggle Here
为什么 Java 开发者常卡在这里
Java normalizes free movement of references. Rust distinguishes very sharply between:
Java 把“引用可以自由移动”这件事默认化了,而 Rust 会非常锋利地区分下面三件事:
- owning a value
拥有一个值。 - borrowing it immutably
不可变借用它。 - borrowing it mutably
可变借用它。
Once that distinction becomes intuitive, the compiler stops feeling hostile.
只要这三种关系开始变得直觉化,编译器就不会再显得像在找茬。