2

Box<> is explained like this on the Rust Book:

... allow you to store data on the heap rather than the stack. What remains on the stack is the pointer to the heap data.

With a description like that, I would expect the described object to be called Heap<> or somethingHeapsomethingelse (DerefHeap, perhaps?). Instead, we use Box.

Why was the name Box chosen?

kikito
  • 51,734
  • 32
  • 149
  • 189
  • [This operation is commonly called "boxing" in languages like Java and C#.](https://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#Boxing) – Joe Sewell May 23 '22 at 13:58
  • The etymology is "putting a (primitive) value into a (reference) box" and I'm pretty sure it originated with [Java](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html). – tzaman May 23 '22 at 14:01
  • @tzaman I don't think so. That terminology is also used in [J](https://stackoverflow.com/questions/24158114/what-are-the-differences-between-rusts-string-and-str), which is older than Java. – jthulhu May 23 '22 at 14:06
  • Cause we needed a name, so Box it is – Stargateur May 23 '22 at 14:33
  • The terminology is also used somewhat in reverse for languages which are semantically universally heap-allocated: "unboxing" in haskell is when the compiler manages to replace a normal (heap-allocated) object with the primitive equivalent and operations. The same with JITs and is quite present in literature e.g. the JIT is able to swap out "normal" objects and operations for primitive ones, the next step being to "explode" composite types in order to operate on their primitive components directly, avoiding 2 layers of allocations and pointer-chasing. – Masklinn May 23 '22 at 16:00
  • 1
    [What is boxing and unboxing and what are the trade offs?](https://stackoverflow.com/q/13055/7884305); [Why is `Box` called `Box`? (users.rust-lang.org)](https://users.rust-lang.org/t/why-is-box-called-box/70522?u=chrefr); [Why Box is named Box? (users.rust-lang.org)](https://users.rust-lang.org/t/why-box-is-named-box/25148?u=chrefr) – Chayim Friedman May 23 '22 at 17:42

1 Answers1

4

First, Heap is a very overloaded term, and importantly a heap is an abstract datastructure often used to implement things like priority queues. Having a type called Heap which is not a heap would be extremely confusing, a good reason to avoid that.

Second, "box" is related to the concept of "boxing" or "boxed" objects, in languages which strongly distinguish between value and reference types e.g. Java or Javascript: https://en.wikipedia.org/wiki/Object_type_(object-oriented_programming), in those a "boxed" type is the heap-allocated version of a value type e.g. int/Integer in java, or number/Number in Javascript.

Rust's Box performs an operation which is similar in spirit. Box also originally had a built-in "lifting" operator called box (it's still an internal operation and was originally planned to be stabilised for placement new), as such "box"/"boxing" makes sense linguistically in a way "heap"/"heaping" really does not (as "heaping" hints at a lot of things being put on a heap).

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • I didn't mean to call it Heap, but *Something*Heap*Something* :). From the wikipedia article: "Boxing, otherwise known as wrapping, is the process of placing a primitive type within an object so that the primitive can be used as a reference object. For example, in Java, a LinkedList can change its size, but an array must have a fixed size. One might desire to have a LinkedList of int, but the LinkedList class only lists references to dynamic objects—it cannot list primitive types, which are value types. To circumvent this, int can be boxed into Integer" – kikito May 23 '22 at 20:33