0
let mut a = vec!["1".to_string(), "2".to_string(), "3".to_string()];
let r = &mut a;
let j = r[0];

Below is the error observed.

error[E0507]: cannot move out of index of `Vec<String>`
 --> src\main.rs:4:13
  |
4 |     let j = r[0];
  |             ^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
  |
help: consider borrowing here
  |
4 |     let j = &r[0];
  |             +

Although, the same works if Vec has elements with Copy type.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Only one mutable reference to a resource is allowed. This prevents the usual problems with race conditions, etc. from occurring. See https://chat.openai.com/share/0bde2a43-7161-4c01-966f-037fdd8d7eb8 – Robert Harvey Jul 01 '23 at 13:28

0 Answers0