Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Function receiving and returning one reference

  • the elision rules apply and thus we don't need lifetime specifiers
fn main() {
    let mut x = String::new();
    println!("{x}");
    x = String::from("before");
    println!("{x}");
    let c = select(&x);
    x = String::from("after");

    println!("{}", c);
    println!("{}", x);
}

fn select(text: &str) -> &'static str {
    if text > "abc" {
        "first"
    } else {
        "second"
    }
}

// fn select(text: &str) -> &str {
//     // fn select<'a>(text: &'a str) -> &'a str {  // the same as above
//     // fn select<'a, 'b>(text: &'a str) -> &'b str { // fully generic
//     // fn select<'b>(text: &str) -> &'b str { // specific lifetime
//     // fn select(text: &str) -> &'static str {
//         if text > "abc" {
//             "first"
//         } else {
//             "second"
//         }
//     }
Foo
Foo