DEV Community

Discussion on: How type conversions work in Rust

timclicks profile image
Tim McNamara Author • Edited on

As described, when changing between values of equal width, it does not change ; what happens if we convert u8 to u16 or to u32? Assuming these are defined on stack, does it repush again with correct width no. Of bytes or it (somehow) store it in register of full width and consider the final width no. Of bytes from there?

I'm not sure about what happens at the machine code level. I could imagine that the optimizer might avoid promoting values to a wider type if it's not necessary, but I don't think that there would be a difference between the data type on the stack and the data type held in registers.

When converting from signed to unsigned or so, would it always crash? I don't remember 100% but we can convert 255 to -1 when going u8 to i8 ?

Actually, I was wrong about this. Programs don't crash, they just silently continue with the wrong value. I have updated the article.