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.