我对Rust还是个新手,正在努力了解拳击的工作原理. 默认情况下,对象在堆栈中分配. 在接下来的代码中,对象"S"被分配到堆栈中.这意味着变量x和y被分配到堆栈中.
struct MyStruct {
x: u64,
y: u64,
}
let s = MyStruct { //allocation in the stack ? both x and y allocated in stack?
x : 120, y: 100,
};
现在,如果我将变量‘S’装箱,它会被移到堆中吗?
let boxed_s = Box::new(s); //move to heap ? copy x and y bytes to heap?
这意味着存在从堆栈到堆的逐字节复制,因为它需要为x和y分配和移动值?
如果是这样的话,这不是造成了开销吗?
或者,编译器是否会在编译时进行一些优化,以确保在运行时将对象‘S’直接分配到堆中?