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
/// # 智能指针:Box<T> /// /// 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(); /// ``` pub fn box_demo(){ #[derive(Debug, PartialEq)] struct Point { x: f64, y: f64, } let box_point = Box::new(Point { x: 0.0, y: 0.0 }); let unboxed_point: Point = *box_point; assert_eq!(unboxed_point, Point { x: 0.0, y: 0.0 }); }