假設我有以下結構
trait T{}
struct A<X:T>{
...
}
我想知道這樣的事情是否可能
Box<A<dyn T>>
目前我收到錯誤
the trait `Sized` is not implemented for `(dyn T 'static)`
但是當我添加
trait T:Sized{}
我明白了
the trait cannot be made into an object because it requires `Self: Sized`
uj5u.com熱心網友回復:
這是可能的,但泛型Sized默認情況下具有系結,這會阻止它們被未調整大小的 trait 物件實體化。您需要指定T: ?Sized:
trait T {}
struct A<X: ?Sized T> {}
例如:
trait T {}
impl T for () {}
struct A<X: ?Sized T> {
value: X,
}
fn foo() {
let _ = Box::<A<()>>::new(A { value: () }) as Box<A<dyn T>>;
}
游樂場。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/437833.html
