serde icon indicating copy to clipboard operation
serde copied to clipboard

"lifetime may not live long enough" when derive Deserialize for an enum with `Vec<&'static str>`

Open Mingun opened this issue 1 year ago • 9 comments

Deriving Deserialize for that struct:


#[derive(Deserialize)]
enum Enum {
    Variant(Vec<&'static str>), // error due to this
}

#[derive(Deserialize)]
enum Enum2 {
    Variant(&'static str), // there is no error
}

gives the following error:

   Compiling playground v0.0.1 (/playground)
error: lifetime may not live long enough
 --> src/lib.rs:5:13
  |
3 | #[derive(Deserialize)]
  |          ----------- lifetime `'de` defined here
4 | enum Enum {
5 |     Variant(Vec<&'static str>),
  |             ^^^ requires that `'de` must outlive `'static`

error: could not compile `playground` due to previous error

At the same time, a variant without Vec compiled ok.

Playground. In comment you can see derived code. Derived code for the Enum2 variant is the same except that all occurrences of 'de is replaced by 'static.

This is a problem also in 1.0.157, but playground did not have it yet at time of writing.

Mingun avatar Mar 18 '23 08:03 Mingun