Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Unique-Immutable Borrow

&uniq T is a new reference type that like &mut T is guaranteed to be the only reference pointing to a given place. Unlike &mut T however, it doesn't allow mutating the pointed value.

The point of this borrow is that a &uniq &mut T is allowed to mutate the underlying T but not the &mut T itself. This is used to desugar closure captures.


Discussion

This borrow actually exists in the compiler for exactly the same reason we need it: for capturing &mut references. It is just not exposed to users.

I'm not sure if this is useful for anything else. I recall from discussions on true reborrowing that possibly a function like fn reborrow<'a, 'b>(x: &'a uniq &'b mut T) -> &'a mut T is more true to what reborrowing does than the same function with a &mut &mut T argument, but I haven't thought this through in detail.