2 ms·
Rust and C# both use 'unsafe { }' blocks, which can only be enabled by a module-level compiler flag.
by jiggawatts 4y ago
Rust and C# both use 'unsafe { }' blocks, which can only be enabled by a module-level compiler flag.
- tialaramex 4y agoThere is no such "compiler flag" in Rust, that's only a C# thing. But also more relevantly here, stuff you'd be calling in those Rust blocks tends to have longer names and often calls out the fact it's specifically not checked, e.g. a = b.unchecked_add(c); // Like a C or C++ arithmetic operation if you overflow it's UB. This might be faster. It might not. But if you must have this anyway, that's how n = NonZeroU32::new_unchecked(0); // Unlike new() which returns None because duh, zero isn't non-zero, this results in UB. Not all of them though, for example: v = Vec::from_raw_parts(ptr, len, cap); // Make a Vec, the pointer ptr had better actually be pointing at contiguous memory for exactly cap item-sized slots, the first len of which are in fact legitimately values of the appropriate type, if any of this is wrong that's immediately UB.
- CamJN 4y agoWhile sadly it does not require a flag to enable, at least you can turn it off with: `#![forbid(unsafe_code)]`.