Skip to content

Instantly share code, notes, and snippets.

@chamakits
Forked from anonymous/playground.rs
Created June 24, 2016 05:11
Show Gist options
  • Save chamakits/be58e64728e1f92c23890243d4595bd5 to your computer and use it in GitHub Desktop.
Save chamakits/be58e64728e1f92c23890243d4595bd5 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
struct StructNoLifetime {}
impl StructNoLifetime {
pub fn do_nothing(&mut self) {
}
}
struct StructWithLifetime <'a> {
pub field: &'a i32
}
fn main() {
let mut some_val = StructNoLifetime{};
some_val.do_nothing();
borrow_mut_function(&mut some_val);
borrow_mut_function(&mut some_val);
let num = 5;
let mut life_val = StructWithLifetime{field:&num};
borrow_lifetime(&mut life_val);
borrow_lifetime(&mut life_val);
let num_again = borrow_lifetime(&mut life_val);
borrow_lifetime(&mut life_val);
}
fn borrow_mut_function(val_in: &mut StructNoLifetime) -> String {
"abc".to_string()
}
fn borrow_lifetime<'a>(val_in: &'a mut StructWithLifetime) -> &'a i32 {
val_in.field
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment