检查字符串切片是否包含在 HashSet<Rc 中<String>>而不分配新的字符串



我正在缓存来自HashSet<Rc<String>>输入的单词。 在检查它是否包含在集合中时,我可以以某种方式使用&str作为此集合的键HashSet<String>吗?

使用HashSet<String>,它可以工作:

use std::rc::Rc;
use std::collections::HashSet;
let input = "";
let string = input.to_string();
let rc_string: Rc<String> = Rc::new(string.clone());
let set: HashSet<String> = [string.clone()].iter().cloned().collect();
assert!(set.contains(&string));
assert!(set.contains(input));

但是如果我尝试使用HashSet<Rc<String>>

let string_cache: HashSet<Rc<String>> = [rc_string.clone()].iter().cloned().collect();
assert!(string_cache.contains(&rc_string));
assert!(string_cache.contains(&string));
assert!(string_cache.contains(input));

然后我得到这个错误:

error[E0277]: the trait bound `std::rc::Rc<std::string::String>: std::borrow::Borrow<str>` is not satisfied
--> src/main.rs:16:26
|
16 |     assert!(string_cache.contains(input));
|                          ^^^^^^^^ the trait `std::borrow::Borrow<str>` is not implemented for `std::rc::Rc<std::string::String>`
|
= help: the following implementations were found:
<std::rc::Rc<T> as std::borrow::Borrow<T>>

如错误消息所述,HashSet::contains要求集合中存储的项的类型具有其参数类型的Borrow实现。没有实现Rc<String>Borrow<str>

您不能自己添加此实现,因为所涉及的类型和特征都不是来自您的板条箱。但是,您可以为Rc<String>创建一个 newtype 包装器,并实现您可能需要的任何Borrow实现:

#[derive(Debug, Eq, PartialEq, Hash)]
struct CacheItem(Rc<String>);
impl Borrow<str> for CacheItem {
fn borrow(&self) -> &str {
&self.0
}
}
impl Borrow<String> for CacheItem {
fn borrow(&self) -> &String {
&self.0
}
}
impl Borrow<Rc<String>> for CacheItem {
fn borrow(&self) -> &Rc<String> {
&self.0
}
}
let string_cache: HashSet<CacheItem> = [rc_string.clone()].iter().cloned().map(CacheItem).collect();
assert!(string_cache.contains(&rc_string));
assert!(string_cache.contains(&string));
assert!(string_cache.contains(input));

像这样构造的 Newtype 包装器应该具有零运行时成本。但是,您可能需要添加一些额外的 trait 实现才能方便地使用它。

最新更新