Skip to content

Rust gives wrong help. (&mut mut Vec<T>) #85765

Closed
@TornaxO7

Description

@TornaxO7

So this is the minimal code:

fn main() {
    let mut test = Vec::new();

    let rofl: &Vec<Vec<i32>> = &mut test;

    rofl.push(Vec::new());
}

If I'm running cargo check on this, than I'm getting the following:

    Checking rust_tmp v0.1.0 (/tmp/rust_tmp)
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
 --> src/main.rs:6:5
  |
4 |     let rofl: &Vec<Vec<i32>> = &mut test;
  |                                --------- help: consider changing this to be a mutable reference: `&mut mut test`
5 |
6 |     rofl.push(Vec::new());
  |     ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

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

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

[Process exited 101]

So the interesting part is this one:

  |   --------- help: consider changing this to be a mutable reference: `&mut mut test`

If I try this:

fn main() {
    let mut test = Vec::new();

    // Notice the second "mut" after the "="
    let rofl: &Vec<Vec<i32>> = &mut mut test;

    rofl.push(Vec::new());
}

Than I'm getting this when running cargo check:

    Checking rust_tmp v0.1.0 (/tmp/rust_tmp)
error: expected expression, found keyword `mut`
 --> src/main.rs:5:37
  |
5 |     let rofl: &Vec<Vec<i32>> = &mut mut test;
  |                                     ^^^ expected expression

error: aborting due to previous error

error: could not compile `rust_tmp`

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

[Process exited 101]

Well I expected that rust would say, that the given type of rofl is wrong or doesn't fit with the value which is gonna be assigned. Something like this:

    Checking rust_tmp v0.1.0 (/tmp/rust_tmp)
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
 --> src/main.rs:7:5
  |
5 |     let rofl: &Vec<Vec<i32>> = &mut test;
  |               --- help: consider changing this to be a mutable reference:
  |                         `&mut Vec<Vec<i32>>`
6 |
7 |     rofl.push(Vec::new());
  |     ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

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

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

[Process exited 101]

Meta

rustc --version --verbose:

rustc 1.54.0-nightly (5c0292654 2021-05-11)
binary: rustc
commit-hash: 5c029265465301fe9cb3960ce2a5da6c99b8dcf2
commit-date: 2021-05-11
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.1
Backtrace

It didn't even build, so I think it's fine if I don't fill this part.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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