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

pub fn raii_demo()

RAII自动资源管理

Base usage: 等价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);
}Run