I would say that they are equivalent. If I’m not mistaken, let statements only reserves space on the stack, and this only increments the stack register.
And on the latter snippet, the compiler would certainly not bother to modify the stack pointer as the type doesn’t change.
I would say that they are equivalent. If I’m not mistaken,
let
statements only reserves space on the stack, and this only increments the stack register.And on the latter snippet, the compiler would certainly not bother to modify the stack pointer as the type doesn’t change.
It is not guaranteed to do that. It could also use a register or be optimized out completly (like in the example you posted in the other comment).
The stack pointer is also not changed for each local variable, but instead for each function call, so it wouldn’t make a difference anyway.
Ok! Thanks for the clarification !
according to godbolt: https://rust.godbolt.org/z/hP5Y3qMPW
use rand::random; pub fn main1() { let mut var : u128; loop { var = random(); } } pub fn main2() { loop { let var : u128 = random(); } }
compiles to:
So yeah, exactly the same thing.
Fascinating! Thank you for sharing this.