我有一个返回 impl 特征的函数:
pub fn new(buf: &[u8]) -> Result<impl Temperature, u8>
有没有办法表明底层结构也实现了 Debug
(通过 #[derive(...)]
),这样我就可以格式化值?
最佳答案
是的,用 +
组合多个特征,就像在特征边界中一样:
use std::fmt::Debug;
trait Foo {}
fn new() -> impl Foo + Debug {
Dummy
}
#[derive(Debug)]
struct Dummy;
impl Foo for Dummy {}
fn main() {
println!("{:?}", new());
}
关于types - 有没有办法表明 impl 特征类型也实现了额外的特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49263023/