相关疑难解决方法(0)

如何从特征对象获取对具体类型的引用?

如何获得Box<B>&B&Box<B>a在此代码变量:

trait A {}

struct B;
impl A for B {}

fn main() {
    let mut a: Box<dyn A> = Box::new(B);
    let b = a as Box<B>;
}
Run Code Online (Sandbox Code Playgroud)

此代码返回错误:

error[E0605]: non-primitive cast: `std::boxed::Box<dyn A>` as `std::boxed::Box<B>`
 --> src/main.rs:8:13
  |
8 |     let b = a as Box<B>;
  |             ^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
Run Code Online (Sandbox Code Playgroud)

traits rust

38
推荐指数
2
解决办法
9184
查看次数

标签 统计

rust ×1

traits ×1