`Box` should probably be called `Kite`

⚓ Rust    📅 2025-11-23    👤 surdeus    👁️ 12      

surdeus

There has been a previous discussion about this: Why is `Box` called `Box`?

From what I've read, the term "boxing" is more like "wrapping" or "packaging", because primitives behave differently from reference types in Java. But in Rust and C++, primitives are "first-class" in the sense that user-defined types typically live on the stack just like int, so we don't use Box for that. We use Box<T> in Rust because we want to move an instance of T to the heap so that the size of T does not contribute to the size of the enclosing type. I'm sure you all know this, but just as a reminder, note how a struct A can be smaller than its field of type B if we use Box:

struct A {
    b: Box<B>,
}

This is crucial in recursive data structures like:

struct List {
    value: i32,
    next: Option<Box<List>>,
}

Otherwise, it would not compile because something like std::mem::size_of::<List>() = size_of::<i32>() + size_of::<Option<List>>() would create an infinite regress.

I find it unintuitive to call this "boxing". When we box something, we expect it to be larger (or at least heavier, if compressed) than the original, right? What Rust does instead is to put the value elsewhere and just hold on to a rope attached to it, like pulling a heavy cart by a rope: you don't carry the load itself, you only hold the rope. I also imagine it as walking a dog or flying a kite, which gives more of a sense of owning the value at the other end.

Yes, ultimately Java does the same thing as Rust when boxing a value, that is, putting the value on the heap. But Java does this only for lightweight primitives, whereas Rust usually does it for heavier objects. So this gives us some sense of how Java ends up doing the same thing, but for a different purpose. Java boxes a value so that it looks no different from other objects. Rust "boxes" something so that it's not too heavy to hold.

I'm not saying we should actually rename Box to Kite, since it's far too late for that now. Just some random thoughts :face_with_monocle:

3 posts - 3 participants

Read full topic

🏷️ Rust_feed