News for hackers. Stuff that matters.
Re:FOSDEM 2025: The state of Rust trying to catch up with Ad... by pjmlp on Monday February 10, 2025 @08:19AM (#42999881)
Just like bounds checking in Rust.

Some type safety conditions can naturally only be checked at run-time.

And actually, checks are done at compile time if the compiler is able to see what is being used on the spot.

Re:FOSDEM 2025: The state of Rust trying to catch up with Ad... by tialaramex on Monday February 10, 2025 @11:34AM (#43002164)
Also, sometimes you can arrange that the "check" is just the natural way to express what you meant in the language, so whether it's a runtime check or not, the ergonomics are improved enormously, it feels natural.

In Rust NonZeroI8::new(n).unwrap() is either a NonZeroI8 or, if n was zero, it panics because we asked to unwrap the Option and that's None.

What's inside NonZeroI8::new ? Literally a cast. When compiled this is nothing at all, no machine code is generated. It is relying on the fact that Option<NonZeroI8> has bit pattern 00000000 for None, and that's also the bit pattern for the integer zero.

So if we ask at compile time, NonZeroI8::new(FOO).unwrap() for some constant FOO, the compiler will, at compile time, transform the FOO bit pattern, if it's all zeroes this code is just a runtime panic, and if it's on the clear through line (e.g. the only code in the main function) by default the compiler says well that's not going to work, here's a compiler error [if you actually want a program which just panics when run you can ask for that with a compiler setting, good luck to you].

If FOO isn't zero then, still at compile time, now we've got a NonZeroI8 with value FOO