Is it possible to have a list of pointers to things I can compare regardless of their types?
This doesn't make sense. How do you "compare" a String
and a File
or a Socket
and a GuiWindowFontFamily
?
The code has some minor issues as well, namely that it lacks any kind of indirection for the trait. See What is the best way to create a heterogeneous collection of objects? for a good overview of how to make a heterogeneous vector when not fighting object safety.
Speaking of object safety, there's a great blog series on the subject, which covers the details of the specific error you are getting.
So, what can we do? For starters, we can be more specific:
let mut x: Vec<Box<PartialEq<u8>>> = Vec::new();
This works because we are saying that everything in the vector can be compared to a u8
, and there's not an open-ended infinite number of possibilities that each may be compared to.
You could also implement some trait that dictates how things should be compared, and then use that:
trait Silly {
fn silly(&self) -> usize;
}
impl Silly for u8 {
fn silly(&self) -> usize { *self as usize }
}
impl Silly for bool {
fn silly(&self) -> usize { 1 }
}
fn main() {
let mut x: Vec<Box<Silly>> = Vec::new();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…