Search code examples
rustbooleancompound-assignment

`AndAssign`, `OrAssign` (`&&=`, `||=`) in Rust?


Does Rust have a short-circuiting AndAssign (OrAssign) operator &&= (||=), or something similar, for bool? These would desugar into:

// x &&= y
if x { x = y; }

// x ||= y
if !x { x = y; }

Is there a shorter way to write these out, or is the longhand the only way?


Solution

  • As of writing, there are no compound assignment operations for boolean AND or OR.

    Note however that if x { x = y; } can be shortened to x = x && y - same with ||.