I feel like the compiler's rules for when to consume and borrow are confusing and arbitrary. This even extends to the language itself, like if let
potentially being borrowing as a future direction in SE-0390, but let
bindings always being consuming, except if the left-hand side is _
.
I made a suggestion a while ago to fix this, which is admittedly kind of unpolished, but I do hope that in general we can get a more robust/precisely-defined system of ownership inference at some point.
It's also worth noting that the property access being consuming seems to contradict SE-0390:
A class or noncopyable struct may declare stored
let
orvar
properties of noncopyable type. A noncopyablelet
stored property may only be borrowed, whereas avar
stored property may be both borrowed and mutated. Stored properties cannot generally be consumed because doing so would leave the containing aggregate in an invalid state.
Looking at SE-0390, it also seems that the ownership of nonescaping closure captures is dependent on whether the binding is declared as let
or var
, regardless of how the closure actually uses the binding. So your closure workaround might be working for entirely unintuitive reasons:
- Capturing an immutable local binding into a nonescaping closure borrows the binding for the duration of the callee that receives the nonescaping closure.
- Capturing a mutable local binding into a nonescaping closure is a mutating use of the binding for the duration of the callee that receives the nonescaping closure.