I have a function that returns an impl trait:
pub fn new(buf: &[u8]) -> Result<impl Temperature, u8>
Is there a way to signal that the underlying struct also implements Debug
(via #[derive(...)]
), so I can format the value?
I have a function that returns an impl trait:
pub fn new(buf: &[u8]) -> Result<impl Temperature, u8>
Is there a way to signal that the underlying struct also implements Debug
(via #[derive(...)]
), so I can format the value?
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());
}