E0301
Rust Programming Language
Severity: MinorWhat it means
Older Rust compilers (we checked version 1.30): error[E0301]: cannot mutably borrow in a pattern guard.
Rust's error index: this error code is no longer emitted by the compiler. Mutable borrows aren't allowed in pattern guards, the if part of a match arm, because matching cannot have side effects.
Today's rustc stops on the same code with error[E0596]: cannot borrow `option` as mutable, as it is immutable for the pattern guard.
Affected Models
- Older rustc versions. Current ones report E0596 instead
How to Fix It
-
Take the change out of the guard. The guard may only look at the value, not change it.
The error index's example calls option.take() in a guard: if that were allowed, the guard could empty the value, and no arm would match.
-
Need the result of the change to pick an arm? Do the change before the match, keep the result in a variable, and match on that, or test it in the guard.