mirror of https://github.com/sunface/rust-course
parent
641ecabf4c
commit
e4b116b173
@ -1 +1,2 @@
|
||||
.DS_Store
|
||||
book
|
@ -1,2 +0,0 @@
|
||||
book
|
||||
.DS_Store
|
@ -1 +0,0 @@
|
||||
## Rust语言圣经随书习题的答案
|
@ -1,51 +0,0 @@
|
||||
## 闭包
|
||||
|
||||
下面代码是Rust圣经课程中[闭包](http://course.rs/advance/functional-programing/closure.html#结构体中的闭包)章节的课内练习题答案:
|
||||
|
||||
```rust
|
||||
struct Cacher<T,E>
|
||||
where
|
||||
T: Fn(E) -> E,
|
||||
E: Copy
|
||||
{
|
||||
query: T,
|
||||
value: Option<E>,
|
||||
}
|
||||
|
||||
impl<T,E> Cacher<T,E>
|
||||
where
|
||||
T: Fn(E) -> E,
|
||||
E: Copy
|
||||
{
|
||||
fn new(query: T) -> Cacher<T,E> {
|
||||
Cacher {
|
||||
query,
|
||||
value: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&mut self, arg: E) -> E {
|
||||
match self.value {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let v = (self.query)(arg);
|
||||
self.value = Some(v);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_with_different_values() {
|
||||
let mut c = Cacher::new(|a| a);
|
||||
|
||||
let v1 = c.value(1);
|
||||
let v2 = c.value(2);
|
||||
|
||||
assert_eq!(v2, 1);
|
||||
}
|
||||
```
|
Before Width: | Height: | Size: 6.6 KiB After Width: | Height: | Size: 6.6 KiB |
Before Width: | Height: | Size: 6.2 KiB After Width: | Height: | Size: 6.2 KiB |
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.1 KiB |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue