-
-
Save Playgirlkaybraz11/ee7242838c39c56dfa645344ebb0dc9b to your computer and use it in GitHub Desktop.
Code shared from the 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<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
@stephencelis #1396