我正在尝试使用。在程序中使用强类型包装器来包装“键”,这样我就不会将任意字符串误认为是键。我有:
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Key(String);
我有一个HashMap<Key, _>
,我想通过引用键类型来查找值(即不必拥有字符串)。看来我需要做的是:#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct KeyRef<'a>(&'a String);
(实际上,我想要KeyRef<'a>(&'a str)
,但是使用String
可以使示例更清晰)Borrow<KeyRef<'_>> for Key
我已经尽力了,这是playground link
我最明确的尝试(注释所有生命)是:
impl<'a> Borrow<KeyRef<'a>> for Key {
fn borrow<'b>(&'b self) -> &'b KeyRef<'a> where 'b: 'a {
let string_ref : &'a String = &self.0;
let key_ref : &'a KeyRef<'a> = &KeyRef(string_ref);
key_ref
}
}
这给了我一个错误:“方法borrow
上的生命周期参数或界限与特征声明不匹配”。凭直觉,这应该是可能的:
'a
的引用,因此KeyRef的任何值都不能超过'a
。 fn borrow<'b>(&'b self)
中,由于上述'b
不能大于'a
但是编译器似乎不喜欢我的显式尝试(使用
where 'b: 'a
)来证明这一点,而将其取消后,由于“需求冲突,我无法推断出借用表达式的适当生存期”
最佳答案
据我了解您的情况,您不必要地使事情复杂化。一个简单的实现:
use std::collections::HashMap;
use std::borrow::Borrow;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Key(String);
impl Borrow<str> for Key {
fn borrow(&self) -> &str {
&self.0
}
}
impl Borrow<String> for Key {
fn borrow(&self) -> &String {
&self.0
}
}
fn main() {
let mut map = HashMap::new();
map.insert(Key("one".to_owned()), 1);
// Because Key is Borrow<String>
println!("{:?}", map.get("one".to_owned()));
// Because Key is Borrow<str>
println!("{:?}", map.get("one"));
}
关于rust - 为具有生存期的类型实现借阅特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65858144/