Tutorial by Examples

fn foo<'a>(x: &'a u32) { // ... } This specifies that foo has lifetime 'a, and the parameter x must have a lifetime of at least 'a. Function lifetimes are usually omitted through lifetime elision: fn foo(x: &u32) { // ... } In the case that a function takes multiple ...
struct Struct<'a> { x: &'a u32, } This specifies that any given instance of Struct has lifetime 'a, and the &u32 stored in x must have a lifetime of at least 'a.
impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } This specifies that Type has lifetime 'a, and that the reference returned by my_function() may no longer be valid after 'a ends because the Type no longer exists to hold self.x.
fn copy_if<F>(slice: &[i32], pred: F) -> Vec<i32> where for<'a> F: Fn(&'a i32) -> bool { let mut result = vec![]; for &element in slice { if pred(&element) { result.push(element); } } result } This s...

Page 1 of 1