Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the lifecycle does not match, but no error is reported?

I tried to make ‘long’ greater than ‘longest’. This obviously should be reported as an error, but it was not reported. Why?

fn test_lifetime<'long, 'longer, 'longest>(a: &'long str, b: &'longer str, c: &'longest str) where
    'long: 'longest
{
    println!("{}, {}, {}", a, b, c);
}

fn main() {
    let longest = String::from("longest");
    {
        let longer = String::from("longer");
        {
            let long = String::from("long");
            test_lifetime(long.as_str(), longer.as_str(), longest.as_str());
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is not an error for two reasons:

  1. The lifetimes 'long, 'longer and 'longest refer to the borrows, not the values in the variables long, longer and longest;
  2. Rust will try to find lifetimes that satisfy certain constraints. This means that if it compiles, it is possible to find lifetimes that do satisfy these constraints.

So, in this case, a possible set of lifetimes that satisfies all the constraints is simply:

 fn main() {
    let longest = String::from("longest");
    {
        let longer = String::from("longer");
        {
            let long = String::from("long");
                                 // ------\                                   
            test_lifetime(       //       |
                long.as_str(),   //       |
                longer.as_str(), //       |-- 'long = 'longer = 'longest
                longest.as_str() //       |
            );                   //       |
                                 // ------/
        }
    }
}

Indeed, longest.as_str() just creates a &str that has to live less than longest, but it can be as short as the one created by long.as_str(). This is also compatible with your constraints, since 'a: 'a for all 'a (subtyping is reflexive).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading