Hello! Help to deal with such a question.

I make two structures, one FullJson , the second BodyJson . FullJson includes BodyJson as one of its fields. And then in the loop function 10 instances of FullJson are created and transferred somewhere further.

At compilation, the error of the “ expected lifetime parameter ” got out ... It was useful to look at the lifetime in the plant, stumbled on <'a>, <' b>, like the error had disappeared. But I try to understand how this thing works ...

Below is the code that eventually turned out.

Do I understand correctly that having created BodyJson <'a> and FullJson <' b>, I marked their lifetime and that BodyJson <'b> has become FullJson's lifetime ? And such a question is why the compiler has requested that I do this: body: & 'b BodyJson <' b> , i.e. indicated two times ' b ?

Link to gist https://gist.github.com/rogerwilcos/70a78af82713779f1574d030f6013c52

#[macro_use] extern crate serde_derive; extern crate serde; #[macro_use] extern crate serde_json; extern crate exonum; use exonum::crypto::{self, CryptoHash, Hash, PublicKey, SecretKey}; #[derive(Serialize, Debug)] struct BodyJson<'a> { pub_key: &'a PublicKey, act_id: &'a str, form_act_id: &'a str, block: &'a str, status: &'a str, } #[derive(Serialize, Debug)] struct FullJson<'b> { network_id: &'b str, protocol_version: &'b str, service_id: &'b str, message_id: &'b str, signature: exonum::crypto::SecretKey, body: &'b BodyJson<'b>, } 

    1 answer 1

    In rust, all links have a lifetime. In simple cases, for example, when using local variables - links, their lifetime can be taken out of context. However, in more complex scenarios, the lifetime of the links must be indicated explicitly. In your case, each of the structures contains many references and for all of them it is necessary to clearly indicate the time of life. When declaring each structure, you can specify pseudo parameters with a name starting with ' , which denote the lifetime. By specifying the lifetime parameter for the reference fields of the structure act_id: &'a str you explicitly specify the lifetime of each link. When creating a reference to BodyJson parameter is specified twice, since the first parameter &'b assigned to the link itself, and the second parameter sets the lifetime parameter 'a for fields within the BodyJson structure.