|Operator
Explanation
| has three unrelated meanings depending on context:
- Binary: bitwise OR between integers, overloadable via
std::ops::BitOr:a | b(BitOris also implemented forbool, makinga | ba non-short-circuiting logical OR). - Pattern alternatives:
Some(1) | Some(2) => ...inside amatcharm (or any refutable pattern position) — "matches if any of these patterns match." Entirely unrelated to bitwise OR; noBitOrimpl is involved. - Closure parameter delimiters:
|x, y| x + y— opens and closes the closure's parameter list, standing in for the parentheses a function's parameter list would use.
The empty-parameter-list closure form uses || (its own token — see
||) rather than | | with a space; the two are not
interchangeable in the grammar.
Usage examples
Bitwise OR between two integers
let mask = 0b0100 | 0b0001; // <- `|` bitwise OR between two integers
Bit manipulation and flags
Combining several flag bits into one value to pass around or compare
against is the bitwise-OR use of | — distinct from |=, which mutates
an existing binding rather than producing a new combined value.
const FLAG_READY: u8 = 0b0000_0001;
const FLAG_LOGGING: u8 = 0b0000_0100;
fn startup_flags(verbose: bool) -> u8 {
if verbose {
FLAG_READY | FLAG_LOGGING // <- `|` combines two bits into one value
} else {
FLAG_READY
}
}
assert_eq!(startup_flags(true), 0b0000_0101);
Building the combined value with | and returning it
(rather than mutating a mut accumulator with |=) fits a function
that hands back a fresh flag set rather than modifying state in place —
see |= for the in-place variant of the same bitwise
operation.
Branching on data (pattern matching)
Inside a match arm, | separates alternative patterns that should all
take the same branch — a different meaning of the same token from
bitwise OR, resolved entirely by position (pattern position vs.
expression position).
enum HttpStatus {
Ok,
Created,
NotFound,
ServerError(u16),
}
fn is_client_error(status: &HttpStatus) -> bool {
match status {
HttpStatus::NotFound => true,
HttpStatus::Ok | HttpStatus::Created => false, // <- `|`: matches either variant
HttpStatus::ServerError(_) => false,
}
}
An or-pattern collapses what would otherwise be two near-identical match arms with the same body, which the Rust Reference documents as a first-class pattern form rather than sugar layered on top of separate arms.