0

I have a struct:

use std::fmt;

struct SomeStruct {
    e1: i32,
    e2: f64,
}

impl fmt::Display for SomeStruct {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({}, {})", self.e1, self.e2)
    }
}

fn printstuff(name: &str, slc: &[SomeStruct]) {
    println!("{} is {}", name, slc);
}

fn main() {
    let cap = [
        SomeStruct { e1: 1, e2: 1.1 },
        SomeStruct { e1: 2, e2: 2.2 },
        SomeStruct { e1: 3, e2: 3.3 },
        SomeStruct { e1: 4, e2: 4.4 },
    ];
    for elem in &cap {
        println!("  {}", elem);
    }
    println!("cap[1]={}", cap[1]);
    println!("cap[2]={}", cap[2]);
    printstuff("cap", cap);
}

I've implemented fmt::Display on it, but when I try to print out a slice or array of SomeStruct, I get a compile error:

error[E0277]: `[SomeStruct]` doesn't implement `std::fmt::Display`
  --> src/main.rs:15:32
   |
15 |     println!("{} is {}", name, slc);
   |                                ^^^ `[SomeStruct]` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `[SomeStruct]`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: required because of the requirements on the impl of `std::fmt::Display` for `&[SomeStruct]`
   = note: required by `std::fmt::Display::fmt`

This makes sense - I haven't implemented Display for [SomeStruct]. How do I do this? I can't find any Rust docs or anything else that would indicate how I could do this.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Ted Middleton
  • 6,859
  • 10
  • 51
  • 71
  • I believe your question is answered by the answers of [Why doesn't Vec implement the Display trait?](https://stackoverflow.com/q/33759072/155423) and [How do I implement a trait I don't own for a type I don't own?](https://stackoverflow.com/q/25413201/155423). All the same logic applies for `&[T]` as `Vec`. If you disagree, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Apr 02 '19 at 20:14
  • [exemple](https://play.integer32.com/?version=stable&mode=debug&edition=2018&gist=bd31e0356f55d1c527f8b60ed01251bb) – Stargateur Apr 02 '19 at 20:48
  • @Shepmaster - I agree, it is answered in https://stackoverflow.com/q/33759072/155423, although the title doesn't suggest that to a rust beginner like me – Ted Middleton Apr 02 '19 at 21:04
  • It's also not quite obvious to me why you need an enclosing type rather than being able to use the type directly in the trait impl, but I'll ask that as a separate question I guess. – Ted Middleton Apr 02 '19 at 21:08
  • @Shepmaster - It's sort of answered in https://stackoverflow.com/q/33759072/155423, kind of. I own the SomeStruct type so I don't see how the second link applies, but the first link at least has a decent explanation for why there is no default and how to implement a work-around. – Ted Middleton Apr 02 '19 at 21:10
  • 1
    You don’t own the `Display` trait or the slice / array type. – Shepmaster Apr 02 '19 at 21:13
  • *The title doesn’t suggest that* — that’s why SO has duplicates: now this question serves as a signpost to people who search for the same terms you used. – Shepmaster Apr 02 '19 at 21:15
  • @Shepmaster - ok sounds good – Ted Middleton Apr 02 '19 at 21:17

0 Answers0