Definition of borrowing

When I read The Book, chapter 4: it states

We call having references as function parameters borrowing

And it goes on with this example

    let mut s = String::from("hello");

    let r1 = &mut s;
    let r2 = &mut s;

    println!("{}, {}", r1, r2);

When compiled, we get this

$ cargo run
   Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0499]: cannot borrow `s` as mutable more than once at a time
 --> src/
4 |     let r1 = &mut s;
  |              ------ first mutable borrow occurs here
5 |     let r2 = &mut s;
  |              ^^^^^^ second mutable borrow occurs here
6 | 
7 |     println!("{}, {}", r1, r2);
  |                        -- first borrow later used here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0499`.
error: could not compile `ownership`.

To learn more, run the command again with --verbose.

The compiler states

4 |     let r1 = &mut s;
  |              ------ first mutable borrow occurs here

Does this mean that a borrow is happening already when defining a reference and it therefore doesn't need to be, as the book describes, "having references as function parameters".

What tickles my mind here is maybe the compiler error, and the "first mutable borrow occurs here".

What is the definition of borrowing? When i say "borrow", am i talking specifically about reference parameters in functions, or is this happening just by referencing a variable?

1 Like

When you borrow something, that is the act of creating a reference to the thing you are borrowing.

Generating the reference is borrowing, and the borrow exists as long as the reference exists. In most (but not all) cases, the reference is created in order to give it as a parameter to a function; it then gets destroyed at the end of the statement.

You can think of &mut as holding an exclusive lock to its referent. You can do what you like with it, and nothing will ever see your work-in-progress or be able to make changes you weren’t expecting until you give up the lock (by dropping the &mut)

So The Book is not accurate when saying borrowing is only when talking about function parameters, but is is more general: borrowing happens when a reference is created.

Yes borrowing is not only about function parameters. And the wording in the book is indeed not optimal here, as this is the first sentence in the “References and Borrowing” chapter that actually uses (and thus introduces) the word “borrowing”.

1 Like

This is an AWESOME example!! Thanks :slight_smile:

That is what I am thinking. The wording should be more general: borrowing is happening when creating a reference to a variable, this can be used when calling a function.

Or something on that line.

You should create an issue against The Book's repo, or even create a PR to improve the wording.

Feedback from people learning the language is incredibly valuable. Whereas someone already used to the language might skim over that line automatically understanding that borrowing is more general than just function parameters, it takes someone with no prior knowledge of the subject to find these sorts of inaccuracies. Thank you for asking this question :slightly_smiling_face:

As an aside, helping improve documentation can also act as a gateway to making bigger contributions (if that's what you want to do, of course, if not that's cool too). It lets you learn the process of introducing changes, introduces you to the humans behind the project, and helps you get over that initial feeling of "I'm just a noob, there's no way I could contribute to something as big the Rust project".


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.