如果我们有一个已经在堆栈上分配的值,那么装箱会将其复制到堆中并转移所有权吗(这是在.NET中工作的方式,但两个副本都将保持活动状态除外)?或者编译器是否足够“聪明”,从一开始就直接在堆上分配它?
struct Foo {
x: i32,
}
fn main() {
// a is allocated on stack?
let a = Foo { x: 1 };
// if a is not used, it will be optimized out
println!("{}", a.x);
// what happens here? will the stack allocated structure
// be moved to heap? or was it originally allocated on heap?
let b = Box::new(a);
}
我不是汇编语言专家,但这看起来似乎是在堆栈上分配的,然后移动:http://pastebin.com/8PzsgTJ1。但我需要得到一个确切知道发生了什么的人的确认。