所有權
所有變數繫結都會在特定「範圍」內有效,在範圍外使用變數會是錯誤:
struct Point(i32, i32); fn main() { { let p = Point(3, 4); println!("x: {}", p.0); } println!("y: {}", p.1); }
We say that the variable owns the value. Every Rust value has precisely one owner at all times.
At the end of the scope, the variable is dropped and the data is freed. A destructor can run here to free up resources.
This slide should take about 5 minutes.
熟悉垃圾回收實作的學員會知道,垃圾回收器是從一組「根」開始尋找所有可存取的記憶體。Rust 的「單一擁有者」原則也是類似的概念。