[英]How to implement a dictionary with generic rust types?
我一直在研究官方的 rust 書,在“閉包”一章中,有一個演示如何使用 rust 使用 structs 和 impl 來使用緩存,然后最后他們說我們在代碼中添加了函數,其中一個開始使用字典以便我們可以擁有多個緩存,我很快就做到了,但后來它說:
當前 Cacher 實現的第二個問題是它只接受帶有一個 u32 類型參數並返回 u32 的閉包。 例如,我們可能希望緩存采用字符串切片並返回 usize 值的閉包結果。 要解決此問題,請嘗試引入更多通用參數以增加緩存功能的靈活性。
現在代碼的相關部分,經過我的修改,它看起來像這樣。
struct Cacher<T>
where
T: Fn(u32) -> u32,
{
calculation: T,
value: HashMap<u32, u32>,
}
impl<T> Cacher<T>
where
T: Fn(u32) -> u32,
{
fn new(calculation: T) -> Cacher<T> {
Cacher {
calculation,
value: HashMap::new(),
}
}
fn value(&mut self, arg: u32) -> u32 {
match self.value.get(&arg) {
Some(n) => *n,
None => {
let v = (self.calculation)(arg);
self.value.insert(arg, v);
v
},
}
}
}
嘗試允許 HashMap 接收通用值,如下所示。
struct Cacher<T, K>
where
T: Fn(K) -> K,
HashMap<K, K>: std::hash::Hash + std::cmp::Eq,
{
calculation: T,
value: HashMap<K, K>,
}
impl<T, K> Cacher<T, K>
where
T: Fn(K) -> K,
HashMap<K, K>: std::hash::Hash + std::cmp::Eq,
{
fn new(calculation: T) -> Cacher<T, K> {
Cacher {
calculation,
value: HashMap::new(),
}
}
fn value(&mut self, arg: K) -> K {
match self.value.get(&arg) {
Some(n) => *n,
None => {
let v = (self.calculation)(arg);
self.value.insert(arg, v);
v
},
}
}
}
我收到以下錯誤
error[E0599]: no method named `get` found for struct `HashMap<K, K>` in the current scope
--> src/main.rs:27:26
|
27 | match self.value.get(&arg) {
| ^^^ method not found in `HashMap<K, K>`
|
= note: the method `get` exists but the following trait bounds were not satisfied:
`K: Eq`
`K: Hash`
error[E0599]: no method named `insert` found for struct `HashMap<K, K>` in the current scope
--> src/main.rs:31:28
|
31 | self.value.insert(arg, v);
| ^^^^^^ method not found in `HashMap<K, K>`
|
= note: the method `insert` exists but the following trait bounds were not satisfied:
`K: Eq`
`K: Hash`
error[E0277]: the trait bound `HashMap<_, _>: Hash` is not satisfied
--> src/main.rs:39:33
|
5 | struct Cacher<T, K>
| ------ required by a bound in this
...
8 | HashMap<K, K>: std::hash::Hash + std::cmp::Eq,
| --------------- required by this bound in `Cacher`
...
39 | let mut expensive_closure = Cacher::new(|num| {
| ^^^^^^^^^^^ the trait `Hash` is not implemented for `HashMap<_, _>`
error[E0599]: no function or associated item named `new` found for struct `Cacher<_, _>` in the current scope
--> src/main.rs:39:41
|
5 | / struct Cacher<T, K>
6 | | where
7 | | T: Fn(K) -> K,
8 | | HashMap<K, K>: std::hash::Hash + std::cmp::Eq,
... |
11 | | value: HashMap<K, K>,
12 | | }
| |_- function or associated item `new` not found for this
...
39 | let mut expensive_closure = Cacher::new(|num| {
| ^^^ function or associated item not found in \`Cacher<_, _>\`
|
::: /home/fraco/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:201:1
|
201 | pub struct HashMap<K, V, S = RandomState> {
| ----------------------------------------- doesn't satisfy `HashMap<_, _>: Hash`
|
= note: the method `new` exists but the following trait bounds were not satisfied:
`HashMap<_, _>: Hash`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `functionall`
我也執行了答案的建議,但現在我不知道如何使用以下代碼啟動 HashMap。
struct Cacher<T, K>
where
T: Fn(K) -> K,
K: Hash + Eq,
{
calculation: T,
value: HashMap<K,K>,
}
impl<T, K> Cacher<T, K>
where
T: Fn(K) -> K,
K: Hash + Eq,
{
fn new(calculation: T) -> Cacher<T, K> {
Cacher {
calculation,
value: HashMap::new(),
}
}
錯誤
error[E0308]: mismatched types
--> src/main.rs:24:20
|
16 | impl<T, K> Cacher<T, K>
| - this type parameter
...
24 | value: HashMap::new(),
| ^^^^^^^^^^^^^^ expected type parameter `K`, found struct `HashMap`
|
= note: expected type parameter `K`
found struct `HashMap<_, _>`
error[E0599]: no method named `get` found for type parameter `K` in the current scope
--> src/main.rs:29:26
|
29 | match self.value.get(&arg) {
| ^^^ method not found in `K`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `get`, perhaps you need to restrict type parameter `K` with it:
|
16 | impl<T, K: SliceIndex> Cacher<T, K>
| ^^^^^^^^^^^^^
error[E0599]: no method named `insert` found for type parameter `K` in the current scope
--> src/main.rs:33:28
|
33 | self.value.insert(arg, v);
| ^^^^^^ method not found in `K`
error: aborting due to 3 previous errors
然后我嘗試了很多不同的東西,但沒有一個對我有用,我在谷歌搜索了一下,但沒有找到我需要的東西,這就是為什么我來找你尋求幫助,知道如何實現代碼。
PS:我很喜歡rust,我明白為什么最喜歡
錯誤信息
error[E0599]: no method named `get` found for struct `HashMap<K, K>` in the current scope
--> src/main.rs:27:26
|
27 | match self.value.get(&arg) {
| ^^^ method not found in `HashMap<K, K>`
|
= note: the method `get` exists but the following trait bounds were not satisfied:
`K: Eq`
`K: Hash`
告訴您HashMap<K,V>::get
僅在 K 實現Eq
和Hash
時存在。 但是您的value
function 沒有對 K 施加任何此類限制。(相反,您嘗試將限制限制在Hash<K,K>
上。只需在where
子句中將它們添加到K
就足夠了:
impl<T, K> Cacher<T, K>
where
T: Fn(K) -> K,
K: std::hash::Hash + std::cmp::Eq
{
但是,這不足以讓您的代碼編譯,您在 value 中所做的事情需要 K 是Copy
- 這也需要添加到where
子句中的限制中。 通常,您會嘗試通過使用Clone
代替或返回引用來減少該要求。
另外:如果您想要更通用的解決方案,您可能應該將其Cacher<T,K,V>
並讓T: Fn(K)->V
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.