Fundamentals19 / 42
Up next

Borrowing (&)

fn char_count(s: &String) -> usize {
    // we borrow s; we do not own it
    s.len()
}

fn main() {
    let s = String::from("hello");
    // lend a reference
    let n = char_count(&s);
    // s is still ours
    println!("{s} has {n} chars");
}
What you're solving

lend a reference without giving ownership