The borrow checker can't prove correctness for all possible valid code. It has some limitations, but it's constantly evolving. In older versions of Rust, it considered values to be borrowed for their entire scope in these cases; a not-so-recent improvement called "non-lexical lifetimes" lifted this restriction and made borrowck smarter. In yet another update called "Polonius", it will be even smarter. Until then, you'll sometimes need to rewrite these patterns in a way that it can prove correctness.
1 Like