【发布时间】:2016-11-03 20:53:20
【问题描述】:
我想要一个包含 Eq 等特征的元素的向量,并且我需要异构向量。例如:
let mut x: Vec<Eq> = Vec::new();
x.push(1);
x.push("hello")
我收到一条错误消息,指出无法将Eq 制成对象:
error[E0038]: the trait `std::cmp::Eq` cannot be made into an object
--> src/main.rs:2:20
|
2 | let mut x: Vec<Eq> = Vec::new();
| ^^ the trait `std::cmp::Eq` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertrait listing
是否有可能有一个指针列表,指向我可以比较的东西,而不管它们的类型如何?
【问题讨论】:
-
如果你乐意使用
Anytrait 而不是Eq,Vec<Box<Any>>可以工作 (playground link)。 -
谢谢,但我确实需要能够比较向量中的东西。特别是,更大的问题是我想要一个具有异构键的 HashMap。
标签: rust