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

Rust ownership - borrow String

  • &}

  • We can tell Rust that a variable borrows the ownership.

  • In this case both variables have (read) access to the variable.

  • We can have as many (read) borrows as we need.

fn main() {
    let x = String::from("Foo Bar");
    println!("{x}");
    let y = &x;
    println!("{y}");
    println!("{x}");
    println!("{y}");
}