I am trying to see if there is any way to implement a vector of sized traits. I know about trait objects, and using Vec<Box<dyn traitName>>
if traitName
is ?Sized
. But what if I have ( https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=43d9ee07cfe643d32a00963aa066c929 ):
trait A: Sized {}
struct B {
c: u64,
}
impl A for B {}
fn lol() -> Vec<A> {
let mut a: Vec<A> = Vec::new();
let b = B { c: 2} ;
a.push(b);
a
}
The error I get is
8 | fn lol() -> Vec<A> {
| ^
|
help: add `dyn` keyword before this trait
but to fix that I have to use trait objects, which is not possible since A: Sized
. Any way to fix this or is it impossible?
A
where A
is a trait is just an old syntax (forbidden in the 2021 edition) to dyn A
. You can't create A
or dyn A
if A: Sized
, ever.
if you'll run this with the 2018 edition, you'll get errors "the size for values of type (dyn A + 'static)
cannot be known at compilation time" .
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.