为什么 Box 在 Rust 中这样命名?

Why is Box called like that in Rust?

Box<> 是这样解释的 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.

有了这样的描述,我希望描述的对象被称为 Heap<>somethingHeapsomethingelseDerefHeap,也许?)。相反,我们使用 Box.

为什么选择 Box 这个名字?

首先,Heap 是一个非常重载的术语,重要的是堆 an abstract datastructure 通常用于实现优先级队列之类的东西。有一个名为 Heap 的类型不是堆会 非常 令人困惑,这是避免这种情况的一个很好的理由。

其次,“盒子”与“装箱”或“装箱”对象的概念相关,在强烈区分值和引用类型的语言中,例如Java 或 Java 脚本:https://en.wikipedia.org/wiki/Object_type_(object-oriented_programming),其中“盒装”类型是值类型的 heap-allocated 版本,例如java 中的 int/Integer 或 Java 脚本中的 number/Number。

Rust 的 Box 执行的操作在本质上是相似的。 Box 最初也有一个 built-in “提升”运算符,叫做 box (它仍然是一个内部操作,最初计划稳定以用于放置新的),如“盒子”/“装箱” " 在语言上以某种方式“堆”/“堆”实际上没有意义(因为“堆”暗示很多东西被放在堆上)。