我想要一個包含諸如Eq
等特徵的元素的向量,我需要異構向量。例如:是否有可能具有實現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
是否有可能有一個指針來的東西我可以比較列表,不論其類型?
如果你樂於使用'Any'特質,而不是'Eq'具體而言,'VEC (作品鏈接)(https://play.rust-lang.org/?gist=22fcc82ccea3f0b0aa0f3528f23d35a6&version=stable&backtrace=0))。 – Aurora0001
謝謝,但我真的需要能夠比較向量中的東西。 特別是,更大的問題是,我想有一個帶有異構鍵的HashMap。 – FCo