[rust-dev] C++ to Rust - Is that about right?

Steve Klabnik steve at steveklabnik.com
Tue Jul 15 04:51:19 PDT 2014


You shouldn't return a Box<T> unless you have to: box does placement
new, so by not doing it yourself, you give them options:

struct Point {
    x: int,
    y: int,
}

impl Point {
    fn bad_new() -> Box<Point> {
        box Point { x: 1, y: 1 }
    }

    fn good_new() -> Point {
        Point { x: 1, y: 1 }
    }
}

fn main() {
   let x = Point::bad_new(); // this _must_ be Box<Point>, and nothing else

   let y = box Point::good_new(); // this is a Box<Point>, with no copy, but...

   let z = box(std::gc::Gc) Point::good_new(); // this is a Gc<Point>,
with no copy.
}


More information about the Rust-dev mailing list