Skip to content

Suggest changing argument mutability #159490

Closed

Description

@estebank

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.
    on Jul 18, 2026
  2. self-assigned this
    on Jul 18, 2026
  3. EvoPot commented on Aug 5, 2026

    @EvoPot
    Contributor

    Quick question unrelated to the issue, how much do we need to specify where the standard library is in the user's system? I am talking about this diagnostic right here:

    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> {
        |            ^^^^^^^^^
    

    Clearly the user shouldn't need to go to the location of the toolchain in their home system. Imagine this for a scenario where people already do know that they should go to somewhere where they have control over the source code like they are going to trace where a file that they are going to change is in std in GitHub or something. I personally think that std should have some more levarage as a central part of the language, which it is for many people. I personally think rather than extra verbosity it should be something more like:

    note: method defined here
       --> std/src/io/stdio.rs:412:12
        |
    412 |     pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
        |            ^^^^^^^^^
    

    Of course this raises a different number of questions (What even is std , what if you are making a fork of the language and change std's name, what if a contributor is debugging with multiple compiler builds (like they have multiple +stage's added to their toolchain) ) but personally I think it is better than giving the end user a full system path for what they shouldn't change. Is there an issue for this? I cannot seem to find one because I lack the expertise in how to find similar issues, but maybe this is a good place to start.

  4. removed their assignment
    on Aug 27, 2026
  5. added 3 commits that reference this issue on Oct 9, 2026
  6. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions