Binary integer literalLiteral
Explanation
A base-2 integer literal, prefixed with 0b, as in 0b1010_0101u8.
Underscores are especially common here to group digits into readable nibbles/bytes, as in that example — purely cosmetic, no effect on the value.
Usage examples
Writing a binary bitmask
let mask = 0b0000_0001; // <- `0b` prefix marks a base-2 (binary) integer literal
Only the digits 0 and 1 may appear after the 0b
prefix — any other digit is a compile error (though underscores and a
type suffix like 0b1010_u8 are still allowed).
Bit manipulation and flags
A single status flag reads clearest written in binary — the bit position is visible directly in the digits, with nothing to mentally translate.
const ENABLED: u8 = 0b0001; // <- binary literal: a single flag bit, position visible in the digits
let status: u8 = 0b0101;
if status & ENABLED != 0 {
println!("device is enabled");
}
Writing a lone flag as 0b0001 keeps the bit position
obvious next to sibling flag constants; once a mask spans several bits at
once, hex reads more compactly — see
integer-hexadecimal for that convention, which
follows the same bitwise semantics documented on the
std BitAnd trait.