3 ms·
In the context of Rust, a bool is a 1-byte value with 2 valid representations (0x00 and 0x01) and 254 invalid representations (disregarding uninit bytes). The c
by LegionMammal978 4y ago
In the context of Rust, a bool is a 1-byte value with 2 valid representations (0x00 and 0x01) and 254 invalid representations (disregarding uninit bytes). The compiler can make use of these invalid representations for niche optimizations: For instance, an Option<bool> (i.e., std::optional<bool>) takes only 1 byte instead of 2, since the compiler can repurpose the invalid byte 0x03 to represent the None (i.e., std::nullopt) state of the Option. The compiler generally tries to perform these niche optimizations for all enums, although it's only 100% guaranteed to happen in certain cases.