Function primitive_types

Source
pub fn primitive_types()
Expand description

§Rust 语义:Move 语义 与 Copy 语义

基本数据类型: https://doc.rust-lang.org/std/index.html#primitives

fn main(){
    // impl Copy for i32
    let a = 42;
    let b = a;
    println!("{:?}", a);  // work

    // impl Copy for &'static str
    let a = "42";
    let b = a;
    println!("{:?}", a); // work

    // impl !Copy for String
    let a = "42".to_string();
    // &String deref to &str
    let b : &str = &a;
    // impl Copy for &'a T
    let c = b;
    println!("{:?}", b); // work

    // impl !Copy for String
    let mut a = "42".to_string();
    // impl !Copy for &mut T
    let b : &mut str = &mut a;
    let c = b;
    // println!("{:?}", b); // don't work, b have been moved

    // auto impl Copy for Tuple, if all item implemented Copy trait in Tuple
    let t = (42, "42");
    let t2 = t;
    println!("{:?}", t); // work

    // auto impl !Copy for Tuple
    let t = (42, "42".to_string());
    let t2 = t;
    // println!("{:?}", t); // don't work, t have been moved
}