rust-lang / rust-lang/book

Book - Chapter 4.2 - Borrowing multiple mutable reference in same scope

Open
#4,459 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
18.3k
Forks
4.1k
Avg merge
14m
Merged PRs (30d)
1

Description

  • I have searched open and closed issues and pull requests for duplicates, using these search terms:

  • I have checked the latest main branch to see if this has already been fixed, in this file:

URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html

Description of the problem:
Able to borrow multiple mutable reference in same scope. It is mentioned in the book that borrowing mutable reference more than once (in same scope) is restricted. But, in the below code, I could borrow mutable reference of s more than once. It compiles and works fine.

fn main() {
let mut s = String::from("hello");
let r1 = &mut s;
r1.push_str("Mutation 1");
let r2 = &mut s;
r2.push_str("Mutation 2");
println!("{s}");

}

Suggested fix:
Issue can be in the book or in compiler. I'm not sure.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Chapter 4.2 section and compare its statement about multiple mutable references with the Rust example in this issue. Check whether the wording or example needs clarification, and consider the issue complete when the book accurately explains why the sequential borrows compile or identifies a concrete correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.