所有权和复制特征
一些 Rust 类型实现了 Copy 特性。可以在不拥有相关值的情况下移动 Copy 的类型。这是因为值的内容可以简单地在内存中逐字节复制,以产生新的相同值。Rust(bool,usize,f64 等)中的大多数原始人都是 Copy。
let x: isize = 42;
let xr = &x;
let y = *xr; // OK, because isize is Copy
// both x and y are owned here
值得注意的是,Vec 和 String 不是 Copy:
let x = Vec::new();
let xr = &x;
let y = *xr; // ERROR, cannot move out of borrowed content