Values & References9 / 18
Up next

Borrow instead of moving big data

// borrowing avoids handing over (or copying) the whole Vec
fn sum(v: &[u64]) -> u64 {
    v.iter().sum()
}

fn main() {
    let data: Vec<u64> = (1..=1000).collect();
    let s = sum(&data);
    // data is untouched and still owned
    println!("{s}, kept {} items", data.len());
}
What you're solving

pass &[T] to avoid moves and copies