Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 5, 2020 14:19
Show Gist options
  • Save rust-play/215203903b9b97f6780cd8d614b9faca to your computer and use it in GitHub Desktop.
Save rust-play/215203903b9b97f6780cd8d614b9faca to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
struct Foo(u32);
struct Bar<'c>(&'c Foo);
impl<'h, 'c: 'h> Bar<'c> {
fn new(foo: &'c mut Foo) -> Bar<'c> {
Bar(foo)
}
fn baz(&'h self) -> Baz<'c> {
Baz(&(self.0).0)
}
}
struct Baz<'c>(&'c u32);
struct Quix<'c>(Bar<'c>, Option<Baz<'c>>);
impl Quix<'_> {
fn setup(&mut self) {
self.1 = Some(self.0.baz())
}
fn mu(&mut self) -> Result<(), Option<u32>> {
Err(self.1.as_ref().map(|s| *s.0))
}
}
fn main() {
let c = || {
let mut foo = Foo(42);
let bar = Bar::new(&mut foo);
let mut quix = Quix(bar, None);
quix.setup();
quix.mu()
};
println!("{:?}", c());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment