Copying and moving values - The copy trait

In the code we looked at above (see Chapter 7/code/lifetimes.rs) the value of n is copied to a new location each time n is assigned via a new let binding or passed as a function argument:

let n = 42u32; 
let n2 = n; // no move, only a copy of the value n to n2 
 
life(n); // copy of the value n to m 
 
fn life(m: u32) -> u32 { 
    let o = m; // copy of the value m to o 
    o 
} 

At a certain moment in the program's execution we would have four memory locations containing the copied value 42, which we could visualize as follows:

Each value disappears (and its memory location is freed) when the lifetime of its corresponding ...

Get Rust Essentials - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.