1

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?

Sajuuk
  • 2,667
  • 3
  • 22
  • 34
  • The first snippet does **not** compile as well as the second one! – hellow Jul 30 '19 at 07:35
  • @hellow that's because I didn't add the rest of fn main which I thought was not helpful. – Sajuuk Jul 30 '19 at 08:05
  • @hellow added complete source code. – Sajuuk Jul 30 '19 at 08:13
  • @Sajuuk The message is clear: *"expected lifetime parameter"*. Did you search for it ? – Denys Séguret Jul 30 '19 at 08:14
  • @DenysSéguret I did quite a lot of search about it, theory be that I need to annotate lifetime for `&str` in `Vec<&str>` like `Vec<&'a str>` but that just brought in more problem. the difference between my sitation and that post is that I have no explicit use for &str in my struct.. – Sajuuk Jul 30 '19 at 08:16
  • @Sajuuk this second QA should help with the additional problems: https://stackoverflow.com/questions/24158114/what-are-the-differences-between-rusts-string-and-str – Denys Séguret Jul 30 '19 at 08:17
  • @Sajuuk *"that's because I didn't add the rest of fn main"*: no! Because of you changed it from `&u8` to `u8`. The first one is a reference, whilst the second one isn't one. That's the difference. – hellow Jul 30 '19 at 08:28

1 Answers1

1

The compiler is told that you're borrowing a value, but not for how long it will live. Should it be static? Something else?

I presume you're trying to do the following.

struct StrDisplayable<'a>(Vec<&'a str>);

This way, you're explicitly telling the compiler that the strings will live at least as long as the struct, no less.

You'll also need to add in a lifetime in the implementation of the trait, which can by anonymous if using Rust 2018.

jhpratt
  • 6,841
  • 16
  • 40
  • 50
  • That's it except a final step to be performed: I have to add anonymous lifetime:``<'_>` after the `StrDisplayable` in `impl` statement before the code can compile. thanks! – Sajuuk Jul 30 '19 at 08:24
  • Yeah, if you're using Rust 2018 (hopefully you are), you should (need?) to add the anonymous lifetime, which can then be inferred by the compiler. – jhpratt Jul 30 '19 at 08:26
  • can you explain why only write like this is not sufficient?: `struct StrDisplayable(Vec<&'a str>);` alsp please edit the <'_> into answer for more clarity perhaps – Sajuuk Jul 30 '19 at 08:26
  • In that case, you're referencing a lifetime that isn't defined anywhere. – jhpratt Jul 30 '19 at 08:27
  • 2
    How does your answer differ from the [suggested duplicate](https://stackoverflow.com/questions/33485203/what-does-missing-lifetime-specifier-mean-when-storing-a-str-in-a-structure)? – hellow Jul 30 '19 at 08:33
  • While the string slices in the vector will only live as long as the struct, it is incorrect to say the same for the strings themselves. The only requirement is that they must only outlive the lifetime `'a`, which can be wider than the lifetime of a value of type `StrDisplayable`. – E_net4 Jul 30 '19 at 08:33
  • @E_net4 Corrected, it now says it lives for at least that long. – jhpratt Jul 30 '19 at 08:35
  • @hellow That dupe was suggested seconds before I posted my answer. – jhpratt Jul 30 '19 at 08:36