3 ms·
A Box is not a pointer. It is an object with regular one-owner, no copies semantics. Semantically, it just breaks the object out so that it isn’t “internal” to
by wwright 6y ago
A Box is not a pointer. It is an object with regular one-owner, no copies semantics. Semantically, it just breaks the object out so that it isn’t “internal” to whatever contains it (useful for keeping individual objects small or working around the requirement that a type have a known, finite size).
A pointer and a heap allocation are the obvious choice for implementing this, but the semantics are notably different than just being a pointer. Rust also does have pointer types which represent true pointers.
- umanwizard 6y agoAFAICT, Box has exactly the same semantics as C++‘s unique_ptr.
- temac 6y agoWhere it can have the same semantics, yes. But if you look at the details, not exactly. For example Box is not nullable, but a not-nullable C++ equivalent is hard to do given the rest of the language.
- umanwizard 6y agoGood point on the nullability; yes, that is indeed different from std::unique_ptr.