Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement