I think one of the priority documentation item is borrow scopes, i.e. how long something is considered to be borrowed. All examples in ownership chapter of TRPL are let bindings.
The following issue is relevant, and I think the issue should get higher priority than P-low.
https://github.com/rust-lang/rust/issues/12032
Here is my understanding of current rules:
- Things are borrowed for the innermost enclosing statement.
- let bindings are borrowed for the innermost enclosing block.
- Tail expression of block is special and is borrowed for the innermost enclosing block of block, not expression. This rule is recursive.
- Implication of 1 is that something borrowed in true branch of if expression is also borrowed in false branch, likewise for different arms of match expression. This will be fixed.
- Implication of 2 is that
let x = y; x
is not equivalent toy
and you may need to introduce or eliminate let bindings to pass the borrow checker. Will this be fixed? - Another implication of 2 is that destructuring let is not equivalent to destructuring match, because match bindings are borrowed for the innermost enclosing statement, not block. This is probably working as designed, as long as we have rule 2.
- Another implication of 1 and 2 is that since things are not borrowed for something less than a statement,
x
is borrowed untilf
returns inf(g(&x))
, not untilg
returns. So you may need to introduce or eliminate temporaries to pass the borrow checker. Will this be fixed?