如何用Display或static绑定Box <dyn trait>的特征?

时间:2020-05-22 11:02:59

标签: rust smart-pointers trait-objects

我出于逻辑目的而实施博客文章。这是GitHub repository的链接。在将结构类型为Option<Box<dyn State>>的结构字段的类型赋予错误时,状态是一个特征,State是要知道帖子当前处于哪个状态的特征,这是我在哪里出错的代码的快速概述。

#[derive(Debug)]
pub struct Post {
    state : Option<Box<dyn State>>,
    content: String,
}

trait State{
    fn request_review(self: Box<Self>)-> Box<dyn State>;
}

输出显示错误,我现在不知道该怎么办。如何解决此问题,先谢谢

error[E0277]: `(dyn trait_object::show::State + 'static)` doesn't implement `std::fmt::Debug`         
 --> src\trait_object.rs:8:5
  |
8 |     state : Option<Box<dyn State>>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn trait_object::show::State + 'static)` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
  |
  = help: the trait `std::fmt::Debug` is not implemented for `(dyn trait_object::show::State + 'static)`
  = note: required because of the requirements on the impl of `std::fmt::Debug` for `std::boxed::Box<(dyn trait_object::show::State + 'static)>`
  = note: required because of the requirements on the impl of `std::fmt::Debug` for `std::option::Option<std::boxed::Box<(dyn trait_object::show::State + 'static)>>`
  = note: required because of the requirements on the impl of `std::fmt::Debug` for `&std::option::Option<std::boxed::Box<(dyn trait_object::show::State + 'static)>>`
  = note: required for the cast to the object type `dyn std::fmt::Debug`

0 个答案:

没有答案