3 ms·
What does unreachable()! do actually? I had no idea that was a thing.
by giancarlostoro 4y ago
What does unreachable()! do actually? I had no idea that was a thing.
- maleldil 4y agoIt terminates the program with panic! https://doc.rust-lang.org/std/macro.unreachable.html https://doc.rust-lang.org/std/macro.unreachable.html
- giancarlostoro 4y agoSo is the usual flow to throw these in places where code shouldn't execute but then create tests to try and trip it up to see if that is truly the case? I would hate to be running a release build with this, or does the compiler do something different depending on build type?
- maleldil 4y ago> I would hate to be running a release build with this The usual argument is that the program would be in an invalid state if the condition was reached, so the only option is to crash. If it turns out it's a valid state, then the programmer can treat in a branch. I don't think tests would capture this because they would operating under the same assumption that such states cannot exist. Maybe fuzzy testing could surface an issue like this.
- remram 4y agoRust has a few of those, they all panic but with different default messages: panic!(), todo!(), unimplemented!(), and unreachable!()