为什么下面的代码是错误的?有一个从[T; N]
到Vec<T>
的全面实现,那么在这个上下文中,为什么dyn Foo
不匹配Fooable
呢?有没有不涉及克隆Fooable
的变通办法?
trait Foo {}
struct Fooable {}
impl Foo for Fooable {}
pub fn main() {
let bar: Vec<Box<dyn Foo>> = [
Box::new(Fooable {}),
].into();
}
错误:
error[E0277]: the trait bound `Vec<Box<dyn Foo>>: From<[Box<Fooable>; 1]>` is not satisfied
--> src/main.rs:10:7
|
10 | ].into();
| ^^^^ the trait `From<[Box<Fooable>; 1]>` is not implemented for `Vec<Box<dyn Foo>>`
|
= help: the following other types implement trait `From<T>`:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T, A> as From<VecDeque<T, A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
<Vec<T> as From<Cow<'a, [T]>>>
<Vec<T> as From<[T; N]>>
<Vec<u8> as From<&str>>
and 2 others
= note: required because of the requirements on the impl of `Into<Vec<Box<dyn Foo>>>` for `[Box<Fooable>; 1]`