This code compiles:
struct IntDisplayable(Vec<u8>);
impl fmt::Display for IntDisplayable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for v in &self.0 {
write!(f, "\n{}", v)?;
}
Ok(())
}
}
fn main() {
let vec: Vec<u8> = vec![1,2,3,4,5];
let vec_Foo = IntDisplayable(vec);
println!("{}",vec_Foo);
}
whilst this code doesn't:
struct StrDisplayable(Vec<&str>);
impl fmt::Display for StrDisplayable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for v in &self.0 {
write!(f, "\n{}", v)?;
}
Ok(())
}
}
fn main() {
let vec: Vec<&str> = vec!["a","bc","def"];
let vec_Foo = StrDisplayable(vec);
println!("{}",vec_Foo);
}
error message:
error[E0106]: missing lifetime specifier
--> src/lib.rs:3:27
|
3 | struct StrDisplayable(Vec<&str>);
| ^ expected lifetime parameter
What I'm trying to do is to implement fmt::Display
for a Vec<&str>
, which generally required wrapping Vec
like this, however it only works for Vec<u8>
, why substitute Vec<u8>
into Vec<&str>
led to such compile error?