Lifetimes in Functions

fn main() {

    let a = String::from("a");
    {
        let b = String::from("bb");
        

        let s1 = longest_1(a.as_str(), b.as_str());

        println!("s1 is {}", s1);


        let s2 = longest_2(a.as_str(), b.as_str());

        println!("s2 is {}", s2);
    }

    //println!("s1 is {}", s);
    //println!("s2 is {}", s);
}

fn longest_1<'a:'b,'b>(x: &'a str, y: &'b str) -> &'b str {

    if x.len() > y.len() {
        x
    } else {
        y
    }
}

fn longest_2<'a>(x: &'a str, y: &'a str) -> &'a str {

    if x.len() > y.len() {
        x
    } else {
        y
    }
}
  • Lifetimes annotations describe the relationships of the lifetimes of multiple references to each other