-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest alternatives when trying to mutate a
HashMap
/BTreeMap
via…
… indexing The error can be quite confusing to newcomers.
- Loading branch information
1 parent
ee8c31e
commit b0255a1
Showing
7 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use std::collections::BTreeMap; | ||
|
||
fn main() { | ||
let mut map = BTreeMap::<u32, u32>::new(); | ||
map[&0] = 1; //~ ERROR cannot assign | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0594]: cannot assign to data in an index of `BTreeMap<u32, u32>` | ||
--> $DIR/btreemap-index-mut.rs:5:5 | ||
| | ||
LL | map[&0] = 1; | ||
| ^^^^^^^^^^^ cannot assign | ||
| | ||
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `BTreeMap<u32, u32>` | ||
= help: to modify a `BTreeMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0594`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let mut map = HashMap::<u32, u32>::new(); | ||
map[&0] = 1; //~ ERROR cannot assign | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0594]: cannot assign to data in an index of `HashMap<u32, u32>` | ||
--> $DIR/hashmap-index-mut.rs:5:5 | ||
| | ||
LL | map[&0] = 1; | ||
| ^^^^^^^^^^^ cannot assign | ||
| | ||
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<u32, u32>` | ||
= help: to modify a `HashMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0594`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters