[][src]Function tao_of_rust::ch05::semantic::value_semantic

pub fn value_semantic()

值语义 == Copy语义 | 引用语义 == Move语义

Base usage: 简单类型就是值语义,也就是Copy语义,因为可以安全在栈上进行复制

let x = 5;
let y = x;
assert_eq!(x, 5);
assert_eq!(y, 5);Run

Base usage: 智能指针一般是引用语义,也就是Move语义,因为不能进行安全栈复制 以下会报错

// error[E0204]: the trait `Copy` may not be implemented for this type
#[derive(Copy, Clone)]
struct A{
    a: i32,
    b: Box<i32>,
}
fn main(){}Run