functional-programing/closure #359
Replies: 5 comments 2 replies
-
为什么第二题的assertion中count == 0 |
Beta Was this translation helpful? Give feedback.
1 reply
-
有没有大佬知到我这个为什么错了,一直提示泛型 struct Cacher<K, V, T>
where
T: Fn(K) -> V,
K: Clone
{
query: T,
value: Option<V>,
}
impl<K, V, T> Cacher<K, V, T>
where
T: Fn(K) -> V,
K: Clone
{
fn new(query: T) -> Cacher<K, V, T> {
Cacher {
query,
value: None,
}
}
// 先查询缓存值 `self.value`,若不存在,则调用 `query` 加载
fn value(&mut self, arg: K) -> V {
match self.value {
Some(v) => v,
None => {
let v = (self.query)(arg);
self.value = Some(v);
v
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_cache() {
let cache = Cacher::new(|x| x + 1);
assert_eq!(cache.value(1), 2);
}
} 输出: Compiling playground v0.0.1 (/playground)
error[E0392]: parameter `K` is never used
--> src/lib.rs:1:15
|
1 | struct Cacher<K, V, T>
| ^ unused parameter
|
= help: consider removing `K`, referring to it in a field, or using a marker such as `PhantomData`
For more information about this error, try `rustc --explain E0392`. |
Beta Was this translation helpful? Give feedback.
0 replies
-
给一个不需要Copy特性的写法 impl<T, E> Cacher<T, E>
} #[test]
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
functional-programing/closure
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/functional-programing/closure.html
Beta Was this translation helpful? Give feedback.
All reactions