trpl-zh-cn/listings/ch13-functional-features/listing-13-10/src/main.rs

35 lines
575 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
struct Cacher<T>
where
T: Fn(u32) -> u32,
{
calculation: T,
value: Option<u32>,
}
// ANCHOR: here
impl<T> Cacher<T>
where
T: Fn(u32) -> u32,
{
fn new(calculation: T) -> Cacher<T> {
Cacher {
calculation,
value: None,
}
}
fn value(&mut self, arg: u32) -> u32 {
match self.value {
Some(v) => v,
None => {
let v = (self.calculation)(arg);
self.value = Some(v);
v
}
}
}
}
// ANCHOR_END: here
fn main() {}