How can I define a vector of comparable in Rust?
Say, i32
, i16
...
I tried arr: Vec<Ord>
but the compiler complains about the trait "std::cmp::Ord" cannot be made into an object
Basically I need to store a vector of a vector of comparable objects. eg
struct Setup<T: Ord + Copy> {
arr: Vec<Vec<T>>
}
impl<T: Ord + Copy> Setup<T> {
fn new() -> Self {
Self {
arr: vec![
vec![1, 2, 3, 4, 5],
vec![1.0, 2.0, 3.0]
]
}
}
}
Instead of letting the consumer decide what exactly the type is, I would like they can get a vector of comparable stuffs.
The type Vec<Ord>
would be a Vec
where each item is a trait object. What you'd want to do is do Vec<T>
and then set the trait bound on T
to be : Ord
, eg
struct Foo<T: Ord> {
arr: Vec<T>,
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.