[][src]Function tao_of_rust::ch02::smart_pointer::box_demo

pub fn box_demo()

智能指针:Box

Basic usage:

fn box_demo(){
    #[derive(PartialEq)]
    struct Point {
      x: f64,
      y: f64,
    }
    // 将Point实例装箱(放到堆内存)
    let box_point = Box::new(Point { x: 0.0, y: 0.0 });
    let unboxed_point: Point = *boxed_point; // 通过解引用操作符取出Point实例
    assert_eq!(unboxed_point, Point { x: 0.0, y: 0.0 });
}
box_demo();Run