1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/// # RAII自动资源管理
///
/// Base usage: 等价C++代码
/// ```c++
/// #include <iostream>
/// #include <memory>
/// using namespace std;
/// int main ()
/// {
///     unique_ptr<int> orig(new int(5));
///     cout << *orig << endl;
///     auto stolen = move(orig);
///     cout << *orig << endl;
/// }
/// ```
///
/// Base usage: 等价Rust代码
/// ```
/// fn main() {
///     let orig = Box::new(5);
///     println!("{}", *orig);
///     let stolen = orig;  // orig 会move,因为是移动语义
///     println!("{}", *orig);
/// }
/// ```
pub fn raii_demo(){
    fn main() {
        let orig = Box::new(5);
        println!("{}", *orig);
        let stolen = orig;
        // println!("{}", *orig);
    }

}

/// # 值语义 == Copy语义 | 引用语义 == Move语义
///
/// Base usage: 简单类型就是值语义,也就是Copy语义,因为可以安全在栈上进行复制
/// ```
/// let x = 5;
/// let y = x;
/// assert_eq!(x, 5);
/// assert_eq!(y, 5);
/// ```
///
/// 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(){}
/// ```
pub fn value_semantic(){
    let x = 5;
    let y = x;
    assert_eq!(x, 5);
    assert_eq!(y, 5);
}

/// # 所有权是否转移
///
/// Base usage: 结构体默认没有实现Copy
/// ```
/// #[derive(Debug)]
/// struct A{
///     a: i32,
///     b: u32,
/// }
/// fn main(){
///     let a = A {a: 1, b: 2};
///     let b = a;  // a的所有权会转移
///     println!("{:?}", a);
/// }
/// ```
///
/// Base usage: 手工为结构体实现Copy
/// ```
/// #[derive(Debug, Copy, Clone)]
/// struct A{
///     a: i32,
///     b: u32,
/// }
/// fn main(){
///     let a = A {a: 1, b: 2};
///     let b = a;  // a的所有权没有转移,因为这里是复制语义
///     println!("{:?}", a);
/// }
/// ```
///
/// Base usage: 元组默认实现Copy
/// ```
/// fn main(){
///     let a = ("a".to_string(), "b".to_string());
///     let b = a;  // String是移动语义,所以此处a会移动
///     // println!("{:?}", a);
///     let c = (1,2,3); // 此处为复制语义
///     let d = c;
///     println!("{:?}", c);
/// }
/// ```
pub fn ownership_change(){
    #[derive(Debug, Copy, Clone)]
    struct A{
        a: i32,
        b: u32,
    }
    fn main(){
        let a = A {a: 1, b: 2};
        let b = a;
        println!("{:?}", a);
    }

}