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

103 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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 ||.