November 2017
Intermediate to advanced
264 pages
5h 45m
English
Every resource is given a name when we make a binding to it with let; in Rust speak we say that the resource gets an owner. For example, in the following code snippet klaatu owns the piece of memory taken up by the Alien struct instance:
// see code in Chapter 7/code/ownership1.rs
struct Alien {
planet: String,
n_tentacles: u32
}
fn main() {
let mut klaatu = Alien{ planet: "Venus".to_string(),
n_tentacles: 15 };
}
Only the owner can change the object it points to, and there can only be one owner at a time, because the owner is also responsible for freeing the object's resources. This makes sense; if an object could have many owners, its resources could be freed more than once, which would lead to problems. When the owner's lifetime ...
Read now
Unlock full access