相关疑难解决方法(0)

特征不能成为一个对象

我有以下代码:

extern crate futures; // 0.1.24

use futures::Future;
use std::io;

struct Context;

pub trait MyTrait {
    fn receive(context: Context) -> Future<Item = (), Error = io::Error>;
}

pub struct MyStruct {
    my_trait: MyTrait,
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我收到错误消息:

error[E0038]: the trait `MyTrait` cannot be made into an object
  --> src/lib.rs:13:5
   |
13 |     my_trait: MyTrait,
   |     ^^^^^^^^^^^^^^^^^ the trait `MyTrait` cannot be made into an object
   |
   = note: method `receive` has no receiver
Run Code Online (Sandbox Code Playgroud)

我想我知道它为什么会发生,但我如何从结构中引用特征呢?可能吗?也许还有其他方法可以实现相同的行为?

generics traits dynamic-dispatch rust trait-objects

17
推荐指数
2
解决办法
1万
查看次数

标签 统计

dynamic-dispatch ×1

generics ×1

rust ×1

trait-objects ×1

traits ×1