我有一个结构体,其中包含一个特质对象成员,就像这样:
我真的不确定如何解决这个问题。我理解为什么编译器无法实现
要获得我想要的行为,应该采用什么方法?
trait Contract {}
#[derive(Debug)]
struct Foo {
x: Box<dyn Contract>,
}
我想让那个结构体派生 Debug
,但编译器不喜欢它:
error[E0277]: `Contract + 'static` doesn't implement `std::fmt::Debug`
--> src/main.rs:5:5
|
5 | x: Box<Contract>,
| ^^^^^^^^^^^^^^^^ `Contract + 'static` cannot be formatted using `:?`; add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
|
= help: the trait `std::fmt::Debug` is not implemented for `Contract + 'static`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::boxed::Box<Contract + 'static>`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&std::boxed::Box<Contract + 'static>`
= note: required for the cast to the object type `std::fmt::Debug`
我真的不确定如何解决这个问题。我理解为什么编译器无法实现
Debug
trait,因为它不能确定哪些类型会实现它,但正是出于同样的原因,让我无法手动为trait实现它(甚至不确定是否可能)。要获得我想要的行为,应该采用什么方法?