What does borrow mean?

error[E0499]: cannot borrow `_owner_itself` as mutable more than once at a time
 --> src\main.rs:7:5
  |
5 |     let/*mut*/_ref_to_owner = &mut _owner_itself; 
  |                               ------------------ first mutable borrow occurs here
6 |
7 |     _owner_itself.push('o');    !!!!!! WHY BORROW TERM ? !!!!!!
  |     ^^^^^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
8 |     _ref_to_owner.push('r');
  |     ----------------------- first borrow later used here

I thought only references can borrow...

Any assignment or potential assignment that involves a lifetime... would be one attempt at a full definition.

There is another reference involved here though. push takes a &mut self. It's implicit in the method call.

There's a more fundamental issue here as well, which I'll try to summarize later (on mobile rn).

2 Likes

The more fundamental issue is that exclusive (&mut) borrows cannot survive use of the thing they borrowed from. And this applies transitively. Rather than try and write this all up from scratch, I'll refer you to these pointers to another thread where mental models for exclusive borrowing are walked through.

As for my vague definition, here's an example where bar involves no actual references, and there's not even an assignment from a to b (however, the API would allow one, and the API is the contract), and yet b is in some sense a borrow of s (or of the unsized str underlying s).

The actual analysis is pretty involved.

2 Likes