sizerustprimitive-types

Is bool guaranteed to be 1 byte?


The Rust documentation is vague on bool's size.
Is it guaranteed to be 1 byte, or is it unspecified like in C++?

fn main() {
    use std::mem;
    println!("{}",mem::size_of::<bool>()); //always 1?
}

Solution

  • Rust emits i1 to LLVM for bool and relies on whatever it produces. LLVM uses i8 (one byte) to represent i1 in memory for all the platforms supported by Rust for now. On the other hand, there's no certainty about the future, since the Rust developers have been refusing to commit to the particular bool representation so far.

    So, it's guaranteed by the current implementation but not guaranteed by any specifications.

    You can find more details in this RFC discussion and the linked PR and issue.

    Please, see E_net4's answer for more information about changes introduced in Rust since this answer had been published.