为什么 Rust 中的堆栈值相差这么远?

Dom*_*iml 2 rust

当我跑

fn main() {
    let x: i32 = 0;
    println!("{:p}", &x);
    let y: i32 = 1;
    println!("{:p}", &y);
}
Run Code Online (Sandbox Code Playgroud)

Rust playground 中,打印的值以十进制形式相隔 88。我的期望是它们相距 4 或 8(字节)。为什么这么大?

Pet*_*all 11

println!宏也将使用堆栈变量。如果您交换语句的顺序(至少在 Rust Playground 调试中),两个指针相距 4 个字节:

fn main() {
    let x: i32 = 0;
    let y: i32 = 1;
    println!("{:p}", &x); // 0x7ffe0b865db0
    println!("{:p}", &y); // 0x7ffe0b865db4
}
Run Code Online (Sandbox Code Playgroud)

无法保证堆栈的使用方式,并且很可能与优化的二进制文件不同。