-
Notifications
You must be signed in to change notification settings - Fork 14k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0fc37a6dfe3b8c2232f2b4eb2ab8615c
struct Board;
fn do_part1(boards: &Vec<Board>) {
let mut boards = boards.clone();
boards[0] = Board;
}The current output is:
warning: variable does not need to be mutable
--> src/lib.rs:4:9
|
4 | let mut boards = boards.clone();
| ----^^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
error[E0596]: cannot borrow `*boards` as mutable, as it is behind a `&` reference
--> src/lib.rs:5:5
|
4 | let mut boards = boards.clone();
| ---------- help: consider changing this to be a mutable reference: `&mut Vec<Board>`
5 | boards[0] = Board;
| ^^^^^^ `boards` is a `&` reference, so the data it refers to cannot be borrowed as mutable
For more information about this error, try `rustc --explain E0596`.
The actual problem here is a missing Clone implementation for Board. Its absence causes Rust to choose the Clone implementation on shared references, so the type of boards here is actually &Vec<Board>; this can be verified by adding an explicit type annotation to the let-bound boards.
The help is not helpful.
It would be much nicer if Rust could somehow point out the missing Clone implementation. Perhaps it should be suspicious of missing mutability and a uselessly cloned shared reference?
jmerdich
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.