有没有办法表明一个 impl trait 类型也实现了额外的 trait? [英] Is there a way to signal that an impl trait type also implements additional traits?
本文介绍了有没有办法表明一个 impl trait 类型也实现了额外的 trait?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个返回 impl 特征的函数:
I have a function that returns an impl trait:
pub fn new(buf: &[u8]) -> Result<impl Temperature, u8>
有没有办法表明底层结构也实现了Debug
(通过#[derive(...)]
),所以我可以格式化该值?
Is there a way to signal that the underlying struct also implements Debug
(via #[derive(...)]
), so I can format the value?
推荐答案
是的,用 +
组合多个 trait,就像在 trait bounds 中一样:
Yes, combine multiple traits with a +
, just like in trait bounds:
use std::fmt::Debug;
trait Foo {}
fn new() -> impl Foo + Debug {
Dummy
}
#[derive(Debug)]
struct Dummy;
impl Foo for Dummy {}
fn main() {
println!("{:?}", new());
}
这篇关于有没有办法表明一个 impl trait 类型也实现了额外的 trait?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文