mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 10:02:42 +00:00
31 lines
1.6 KiB
Rust
31 lines
1.6 KiB
Rust
// ./src/scope/lifetime.md
|
|
|
|
|
|
// Lifetimes are annotated below with lines denoting the creation
|
|
// and destruction of each variable.
|
|
// `i` has the longest lifetime because its scope entirely encloses
|
|
// both `borrow1` and `borrow2`. The duration of `borrow1` compared
|
|
// to `borrow2` is irrelevant since they are disjoint.
|
|
fn part0() {
|
|
let i = 3; // Lifetime for `i` starts. ────────────────┐
|
|
// │
|
|
{ // │
|
|
let borrow1 = &i; // `borrow1` lifetime starts. ──┐│
|
|
// ││
|
|
println!("borrow1: {}", borrow1); // ││
|
|
} // `borrow1 ends. ──────────────────────────────────┘│
|
|
// │
|
|
// │
|
|
{ // │
|
|
let borrow2 = &i; // `borrow2` lifetime starts. ──┐│
|
|
// ││
|
|
println!("borrow2: {}", borrow2); // ││
|
|
} // `borrow2` ends. ─────────────────────────────────┘│
|
|
// │
|
|
} // Lifetime ends. ─────────────────────────────────────┘
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|