rust-lang / rust-lang/rust

Suggest changing argument mutability

Open
#159,490 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-lack-of-suggestion D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    let input = String::new();
    std::io::stdin().read_line(&input).unwrap();
}
Current output
error[E0308]: mismatched types
   --> src/main.rs:5:27
    |
  5 |     io::stdin().read_line(&input).unwrap();
    |                 --------- ^^^^^^ types differ in mutability
    |                 |
    |                 arguments to this method are incorrect
    |
    = note: expected mutable reference `&mut String`
                       found reference `&String`
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:412:12
    |
412 |     pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
    |            ^^^^^^^^^
Desired output
error[E0308]: mismatched types
   --> src/main.rs:5:27
    |
  5 |     io::stdin().read_line(&input).unwrap();
    |                 --------- ^^^^^^ types differ in mutability
    |                 |
    |                 arguments to this method are incorrect
    |
    = note: expected mutable reference `&mut String`
                       found reference `&String`
note: method defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:412:12
    |
412 |     pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
    |            ^^^^^^^^^
help: change the borrow to be mutable
    |
  5 |     io::stdin().read_line(&mut input).unwrap();
    |                            +++
Rationale and extra context

No response

Other cases

Rust Version
1.97
Anything else?

No response

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

Reproduce the example in src/main.rs with Rust 1.97 and compare the current diagnostic with the desired output. Use the stdio.rs signature shown in the error as the API context, then locate the compiler diagnostic code responsible for mismatched reference mutability. Done means the diagnostic suggests changing the borrow to mutable without regressing other E0308 cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.