Lifetimes

  • Lifetimes are a way to manage and ensure memory safety by defining the scope for which a reference is valid.

  • Prevent dangling references and ensure that references don't outlive the data they point to.

  • Lifetime are denoted using (') followed by a name, like 'a.

  • Most of the time, we don't need to explicitly annotate lifetimes because Rust has lifetime elision rules to infer lifetimes in simple cases.

lifetimes1.rs

// The Rust compiler needs to know how to check whether supplied references are
// valid, so that it can let the programmer know if a reference is at risk of
// going out of scope before it is used. Remember, references are borrows and do
// not own their own data. What if their owner goes out of scope?

// Fix the compiler error by updating the function signature.
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_longest() {
        assert_eq!(longest("abcd", "123"), "abcd");
        assert_eq!(longest("abc", "1234"), "1234");
    }
}
  • This exercise is a bit easy.

  • Rust cannot infer the lifetime of input reference in function longest.

  • So we need to define lifetime explicitly by adding 'a (you can change the lifetime name with anything you want).

  • This is to bind the lifetime of input x and y into the output of longest function.

lifetimes2.rs

  • This exercise is easy.

  • The println macro is using result variables which already out of scope (rust already drop it) so we cannot access it anymore.

  • We just need to move the line to inside the bracket.

lifetimes3.rs

  • In this exercise we have struct Book that hold a reference.

  • So we ned to explicitly define lifetime of each reference in the struct so the struct doesn't outlive the reference.

  • We can do it like this:

Last updated