Created
February 17, 2018 10:08
-
-
Save bn3t/400fc8655e3c839da8f0434e9ffd4168 to your computer and use it in GitHub Desktop.
Code from http://arthurtw.github.io/2014/11/30/rust-borrow-lifetimes.html in a version that works with modern Rust (2018).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_variables, unused_assignments, unused_mut)] | |
use std::boxed::Box; | |
struct Foo { | |
f: Box<i32>, | |
} | |
// Owner cannot access resource during a mutable borrow - 1 | |
fn main() { | |
let mut a = Foo { f: Box::new(0) }; | |
// mutable borrow | |
let x = &mut a; | |
// error: cannot borrow `a.f` as immutable because `a` is also borrowed as mutable | |
//println!("{}", a.f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::boxed::Box; | |
#[derive(Debug)] | |
struct Foo { | |
f: Box<i32>, | |
} | |
// Owner cannot access resource during a mutable borrow - 2 | |
fn main() { | |
let mut a = Foo { f: Box::new(0) }; | |
{ | |
// mutable borrow | |
let x = &mut a; | |
println!("{:?}", x); | |
// mutable borrow ends here | |
} | |
println!("{}", a.f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_variables, unused_assignments, unused_mut)] | |
use std::boxed::Box; | |
struct Foo { | |
f: Box<i32>, | |
} | |
// Borrower can move the mutable borrow to a new borrower | |
fn main() { | |
let mut a = Foo { f: Box::new(0) }; | |
// mutable borrow | |
let x = &mut a; | |
// move the mutable borrow to new borrower y | |
let y = x; | |
// error: use of moved value: `x.f` | |
//println!("{}", x.f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_variables, unused_assignments, unused_mut)] | |
use std::boxed::Box; | |
#[derive(Debug)] | |
struct Foo { | |
f: Box<i32>, | |
} | |
// Borrow formula | |
fn main() { | |
let mut a = Foo { f: Box::new(0) }; | |
let y: &Foo; | |
if false { | |
// borrow | |
let x = &a; | |
// share the borrow with new borrower y, hence extend the borrow scope | |
y = x; | |
} | |
// error: cannot assign to `a.f` because it is borrowed | |
// a.f = Box::new(1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_variables, unused_assignments, unused_mut)] | |
use std::boxed::Box; | |
struct Foo { | |
f: Box<i32>, | |
} | |
fn max<'a>(x: &'a Foo, y: &'a Foo) -> &'a Foo { | |
if x.f > y.f { x } else { y } | |
} | |
// Named borrow scope | |
fn main() { | |
let a = Foo { f: Box::new(1) }; | |
let y: &Foo; | |
if false { | |
let b = Foo { f: Box::new(0) }; | |
let x = max(&a, &b); | |
// error: `b` does not live long enough | |
// y = x; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_variables, unused_assignments, unused_mut, dead_code)] | |
use std::boxed::Box; | |
struct Foo { | |
f: Box<i32>, | |
} | |
struct Link<'a> { | |
link: &'a Foo, | |
} | |
// Struct to borrow multiple resources | |
fn main() { | |
let a = Foo { f: Box::new(0) }; | |
let mut x = Link { link: &a }; | |
if false { | |
let b = Foo { f: Box::new(1) }; | |
// error: `b` does not live long enough | |
// x.link = &b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_variables, unused_assignments, unused_mut, dead_code)] | |
use std::boxed::Box; | |
struct Foo { | |
f: Box<i32>, | |
} | |
struct Link<'a> { | |
link: &'a Foo, | |
} | |
fn store_foo<'a>(x: &mut Link<'a>, y: &'a Foo) { | |
x.link = y; | |
} | |
// Function to extend borrow scope without a return value | |
fn main() { | |
let a = Foo { f: Box::new(0) }; | |
let x = &mut Link { link: &a }; | |
if false { | |
let b = Foo { f: Box::new(1) }; | |
// store_foo(x, &b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment