Strings In Depth18 / 18
Up next

A struct that borrows text

// the struct borrows text owned elsewhere, so it carries 'a
struct Token<'a> {
    text: &'a str,
}

fn main() {
    let src = String::from("let x = 1");
    let t = Token { text: &src[0..3] };
    println!("{}", t.text);
}
What you're solving

a &str field needs a lifetime