Rust tips @rust@octodon.social
Follow

won't let you cut the branch you're sitting on.

Sometimes you need to change a complex struct or enum based on its contents. However, you can't do this inside a `match` that takes references, because the references inside the `match` keep a "lock" on the whole object and prevent it from being replaced.

The solution is to use `match` only to check the condition, and perform replacement in a separate step after the `match`.

It's better explained with code:

play.rust-lang.org/?gist=f7616

· Web · 1 · 1