operator

Rust &: keep selected bits

Integer `&` keeps a bit only where both operands have a one. A mask is a number selecting particular bit positions. Expected output (not executed): 0010 true Why: The second-lowest bit is set in both operands. Common mistake: Do not confuse binary `&` with a shared borrow or `&&`. On bool, `&` is eager logical AND. Advanced connection: `std::ops::BitAnd`; `BitAndAssign` for `&=`.

Formal Syntax & Structure

rust syntaxrust
a & b

Working Example

Example Snippetrust
fn main() {    let flags = 0b1011_u8;    let mask = 0b0010_u8;    println!("{:04b}", flags & mask);    println!("{}", flags & mask != 0);}
Rust Reference