Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 7, 2019 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rust-play/1e96ee2360530ea6237643befbbacf4a to your computer and use it in GitHub Desktop.
Save rust-play/1e96ee2360530ea6237643befbbacf4a 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());
}
@ehzrk
Copy link

ehzrk commented Mar 5, 2023

#>admin `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment