Skip to content

Instantly share code, notes, and snippets.

@ccmmff11447722
Forked from rust-play/playground.rs
Last active August 9, 2019 15:34
Show Gist options
  • Save ccmmff11447722/af3b39b6e13e1f678757e3a292d2eaf5 to your computer and use it in GitHub Desktop.
Save ccmmff11447722/af3b39b6e13e1f678757e3a292d2eaf5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
struct Foo<T, C> where C: Fn() -> T {
make_t: C,
value: Option<T>,
}
impl<T, C> Foo<T, C> where C: Fn() -> T {
fn new(make_t: C) -> Foo<T, C> {
Foo {
make_t,
value: None
}
}
fn value(&mut self) -> &T {
self.value
.get_or_insert_with(&self.make_t)
}
}
fn main() {
let mut bar = Foo::new(|| 3);
println!("{}", bar.value());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment