Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Cacher<T, U>
- where T: Fn(U) -> U
- {
- calculation: T,
- value: Option<U>,
- }
- impl<T, U> Cacher<T, U>
- where T: Fn(U) -> U
- {
- fn new(calculation: T) -> Cacher<T, U> {
- Cacher {
- calculation,
- value: None,
- }
- }
- fn value(&mut self, arg: U) -> U {
- match self.value {
- Some(v) => v,
- None => {
- let v = (self.calculation)(arg);
- self.value = Some(v);
- v
- },
- }
- }
- }
- fn main() {
- let mut cash = Cacher::new(|x| {x+1});
- println!("Hello, world! {}", cash.value(10));
- }
Add Comment
Please, Sign In to add comment