Need help with lifetime declaration


#1

I tried but can’t resolve this problem:

#[derive(Serialize, Deserialize)]
pub struct PhotoData<'a> {
pub filename: String,
pub title_en: String,
pub description_en: String,
pub title_ja: String,
pub description_ja: String,
pub tags: Vec<&'a str>,
}

with compiler error:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'de as defined on the impl at 13:21...
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
note: ...so that types are compatible (expected web::_IMPL_SERIALIZE_FOR_Photos::_serde::de::SeqAccess<'_>, found web::_IMPL_SERIALIZE_FOR_Photos::_serde::de::SeqAccess<'de>)
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 13:21...
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
note: ...so that types are compatible (expected web::_IMPL_SERIALIZE_FOR_Photos::_serde::Deserialize<'_>, found web::_IMPL_SERIALIZE_FOR_Photos::_serde::Deserialize<'_>)
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime 'de as defined on the impl at 13:21...
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
note: ...so that types are compatible (expected web::_IMPL_SERIALIZE_FOR_Photos::_serde::de::MapAccess<'_>, found web::_IMPL_SERIALIZE_FOR_Photos::_serde::de::MapAccess<'de>)
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 13:21...
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^
note: ...so that types are compatible (expected web::_IMPL_SERIALIZE_FOR_Photos::_serde::Deserialize<'_>, found web::_IMPL_SERIALIZE_FOR_Photos::_serde::Deserialize<'_>)
  --> src\photo_actions.rs:13:21
   |
13 | #[derive(Serialize, Deserialize)]
   |                     ^^^^^^^^^^^

error: aborting due to 2 previous errors

Could anyone help? Thanks!


#2

This thread should help.


#3

@vitalyd Yup, that works for me. Thanks! :slight_smile: