rust - 如何取消引用 Uuid 类型?
问题描述
我正在使用Uuid crate提供唯一的 id 来实例化Node
具有唯一标识符的结构的每个新版本。有时我想过滤这些结构, .contains()
以检查结构id
是否在某个Vec<Uuid>
.
use uuid::Uuid;
struct Node {
id: Uuid,
}
impl Node {
fn new() -> Self {
let new_obj = Node {
id: Uuid::new_v4()
};
new_obj
}
fn id(&self) -> Uuid {
self.id
}
}
fn main() {
let my_objs = vec![
Node::new(),
Node::new(),
Node::new(),
Node::new(),
];
let some_ids = vec![my_objs[0].id(), my_objs[3].id()];
}
fn filter_objs(all_items: &Vec<Node>, to_get: &Vec<Uuid>){
for z in to_get {
let wanted_objs = &all_items.iter().filter(|s| to_get.contains(*s.id()) == true);
}
}
然而,这给出了错误:
error[E0614]: type `Uuid` cannot be dereferenced
--> src/main.rs:32:72
|
32 | let wanted_objs = &all_items.iter().filter(|s| to_get.contains(*s.id()) == true);
| ^^^^^^^
如何启用Uuid
类型的取消引用来解决这个问题?
解决方案
Uuid
没有实现Deref
特征,所以它不能被取消引用,也不需要因为你试图将它作为参数传递给期望引用的函数。如果更改*s.id()
为&s.id()
代码编译:
fn filter_objs(all_items: &Vec<Node>, to_get: &Vec<Uuid>) {
for z in to_get {
let wanted_objs = &all_items
.iter()
// changed from `*s.id()` to `&s.id()` here
.filter(|s| to_get.contains(&s.id()) == true);
}
}
推荐阅读
- selenium - 要求硒 *find_element_by_class_name
- html - 文本超出网格大小
- javascript - 为什么javascript条件失败随机发生而没有错误?
- python - 我需要从文本分析中获得的结果中消除标点符号
- java - 当尝试 mvn clean install -> 出现错误包不存在
- algorithm - 是否存在从表达式中提取子表达式的算法?
- c++ - 未定义的引用 ld 链接器。(C++)
- ajax - PHPMailer Ajax:文件附件
- reactjs - 页面之间的 React Js 路由中的意外行为
- model - IndexError:PyTorch 中的 Transformers 上的尺寸超出范围(预计在 [-2, 1] 的范围内,但得到了 2)