1
我正在编写Rust中并发字典的基本结构,首先简单地将现有的HashMap包装在Arc::new(Mutex::new(hash_map_placeholder))
中。然而,几乎一旦我开始,事情就开始出现问题。我在将<K, V>
的值传递给一个普通的HashMap时遇到了问题,所以我甚至无法从包装的版本开始。我目前得到以下错误:使用泛型类型时使用“未声明的类型名称”
concurrent_dictionary.rs:11:39: 11:40 error: use of undeclared type name `K`
concurrent_dictionary.rs:11 impl Default for ConcurrentDictionary<K, V> {
^
concurrent_dictionary.rs:11:42: 11:43 error: use of undeclared type name `V`
concurrent_dictionary.rs:11 impl Default for ConcurrentDictionary<K, V> {
我知道这与类型名称没有正确传递有关。如何做到这一点?即使我摆脱了默认的impl,我仍然需要为ConcurrentDictionary::new()
写同样的东西。下面是代码:
use std::collections::HashMap;
use std::default::Default;
#[derive(Clone)]
pub struct ConcurrentDictionary<K, V> {
data: HashMap<K, V>,
}
impl Default for ConcurrentDictionary<K, V> {
#[inline]
fn default() -> ConcurrentDictionary<K, V> {
ConcurrentDictionary {
data: HashMap::<K, V>::new(),
}
}
}
impl<K, V> ConcurrentDictionary<K, V> {
#[inline]
pub fn new() -> ConcurrentDictionary<K, V> {
Default::default()
}
}
完美,谢谢!我只是想让语法正确。现在我有一个文档测试错误,但我非常希望我能自己找出一个。 – NuclearAlchemist
@核化学家可以自由地回来寻找/问另一个问题,如果你被困在任何东西! – Shepmaster