Closed
Description
I was messing around with this function
fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
// BTreeMap instead of HashMap to get a sorted output
let mut map: BTreeMap<_, Vec<_>> = BTreeMap::new();
for item in &m.items {
if item.is_stripped() {
continue;
}
let short = item.type_();
let myname = match item.name {
None => continue,
Some(ref s) => s.to_string(),
};
let short = short.to_string();
map.entry(short)
.or_default()
.push((myname, Some(plain_summary_line(item.doc_value()))));
}
if self.shared.sort_modules_alphabetically {
for (_, items) in &mut map {
items.sort();
}
}
map
}
After I changed the last block to
if self.shared.sort_modules_alphabetically {
for items in &mut map.values() {
items.sort();
}
}
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -1542,7 +1542,7 @@ impl Context {
}
if self.shared.sort_modules_alphabetically {
- for (_, items) in &mut map {
+ for items in &mut map.values() {
items.sort();
}
}
The compiler gave this interesting but wrong suggestion: &mut mut map.values()
error[E0596]: cannot borrow `*items` as mutable, as it is behind a `&` reference
--> src/librustdoc/html/render.rs:1546:17
|
1545 | for items in &mut map.values() {
| ----------------- help: consider changing this to be a mutable reference: `&mut mut map.values()`
1546 | items.sort();
| ^^^^^ `items` 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`.
which does not compiler obviously 😄
The correct solution would be to use &mut map.values_mut()
:
if self.shared.sort_modules_alphabetically {
for items in &mut map.values_mut() {
items.sort();
}
}
```