November 2018
Intermediate to advanced
390 pages
10h 8m
English
Owned values can be borrowed in Rust to allow them to be used for a certain period of time. Borrows can be nested, and borrowed values can become owned values through cloning (using a command such as v.clone).
The following code snippet shows an example of borrowing. The & symbol means borrowed reference:
fn helper(slot: &Vec<int> { /* ... */ }fn main() { let a = Vec::new();// doesn't move! helper(&a); helper(&a); }
Borrowed values are only valid for a particular lifetime:
let a: ∫{ let b = 3; a = &b; // error! 'b' does not live long enough}Let a: ∫let b =3;a = &b; // ok, 'b' has the same lifetime as 'a'
Read now
Unlock full access