Created
March 30, 2017 20:27
-
-
Save anonymous/460f56a66d486d119e3cff356d2b03c5 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or 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
struct Foo; | |
struct FooBuilder<'a> { | |
foo: &'a Foo, | |
} | |
impl Foo { | |
fn build(&self) -> FooBuilder { | |
FooBuilder { foo: &self } | |
} | |
} | |
impl<'a> FooBuilder<'a> { | |
// correct: other lifetime for self | |
fn enable<'b>(&'b mut self) -> &'b mut FooBuilder<'a> { | |
self | |
} | |
// wrong: can only call once | |
fn disable(&'a mut self) -> &'a mut FooBuilder { | |
self | |
} | |
} | |
fn main() { | |
let foo = Foo {}; | |
let mut builder = foo.build(); | |
builder.enable(); | |
builder.disable(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment