Files
rust/compiler
Matthias Krüger 6c22e0419f Rollup merge of #111403 - y21:suggest-slice-swap, r=compiler-errors
suggest `slice::swap` for `mem::swap(&mut x[0], &mut x[1])` borrowck error

Recently saw someone ask why this code (example slightly modified):
```rs
fn main() {
  let mut foo = [1, 2];
  std::mem::swap(&mut foo[0], &mut foo[1]);
}
```
triggers this error and how to fix it:
```
error[E0499]: cannot borrow `foo[_]` as mutable more than once at a time
 --> src/main.rs:4:33
  |
4 |     std::mem::swap(&mut foo[0], &mut foo[1]);
  |     -------------- -----------  ^^^^^^^^^^^ second mutable borrow occurs here
  |     |              |
  |     |              first mutable borrow occurs here
  |     first borrow later used by call
  |
  = help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
```
The current help message is nice and goes in the right direction, but I think we can do better for this specific instance and suggest `slice::swap`, which makes this compile
2023-06-30 08:01:12 +02:00
..
2023-05-02 10:45:16 -07:00
2023-06-28 10:52:48 +08:00
2023-06-29 11:45:52 +10:00
2023-06-05 16:01:09 +02:00
2023-06-11 22:45:04 -04:00
2023-05-26 15:18:54 -04:00
2023-04-10 17:24:23 +09:00
2023-06-28 01:55:32 -04:00
2023-04-10 22:02:52 +02:00