使用编程语言 Rust 进行实验,我发现编译器能够非常准确地跟踪堆栈上某个结构的字段的移动(它确切地知道移动了哪个字段)。但是,当我将结构的一部分放入 a Box
(即,将其放入堆中)时,编译器不再能够确定在取消引用框后发生的所有事情的字段级移动。它将假设“盒子内”的整个结构已经移动。让我们首先看一个所有内容都在堆栈中的示例:
struct OuterContainer {
inner: InnerContainer
}
struct InnerContainer {
val_a: ValContainer,
val_b: ValContainer
}
struct ValContainer {
i: i32
}
fn main() {
// Note that the whole structure lives on the stack.
let structure = OuterContainer {
inner: InnerContainer {
val_a: ValContainer { i: 42 },
val_b: ValContainer { i: 100 }
}
};
// Move just one field (val_a) of the inner container.
let move_me = structure.inner.val_a;
// We can still borrow the other field (val_b).
let borrow_me = &structure.inner.val_b;
}
现在是相同的示例,但有一个细微的变化:我们将InnerContainer
放入一个盒子 ( Box<InnerContainer>
)。
struct OuterContainer {
inner: Box<InnerContainer>
}
struct InnerContainer {
val_a: ValContainer,
val_b: ValContainer
}
struct ValContainer {
i: i32
}
fn main() {
// Note that the whole structure lives on the stack.
let structure = OuterContainer {
inner: Box::new(InnerContainer {
val_a: ValContainer { i: 42 },
val_b: ValContainer { i: 100 }
})
};
// Move just one field (val_a) of the inner container.
// Note that now, the inner container lives on the heap.
let move_me = structure.inner.val_a;
// We can no longer borrow the other field (val_b).
let borrow_me = &structure.inner.val_b; // error: "value used after move"
}
我怀疑它与堆栈的性质与堆的性质有关,前者是静态的(至少每个堆栈帧),而后者是动态的。也许编译器需要安全地发挥它,因为某种原因我不能很好地表达/识别。