1

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Michael Böckling
  • 7,341
  • 6
  • 55
  • 76

1 Answers1

4

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());
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366