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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.