Skip to content
Justice Tech School

Lifetimes Intro

Lifetimes tell the compiler how long references live. Most are inferred.

Example
fn longest<'a>(a: &'a str, b: &'a str) -> &'a str { if a.len() >= b.len() { a } else { b } }

fn main() {
    println!("{}", longest("apple", "fig"));
}

Result

apple

Exercise

Lifetime mark.

fn f< a>(x: &a str)

Quiz Test

What do lifetimes describe?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.