Closed
Description
Let's say you have the following (simplified) code: (Rust Playground)
fn main() {
let items = vec![1, 2, 3];
let ref_items: &[i32] = &items;
let items_clone: Vec<i32> = ref_items.clone();
}
This results in the following error:
error[E0308]: mismatched types
--> src/main.rs:4:33
|
4 | let items_clone: Vec<i32> = ref_items.clone();
| ^^^^^^^^^^^^^^^^^
| |
| expected struct `std::vec::Vec`, found &[i32]
| help: try using a conversion method: `ref_items.clone().to_vec()`
|
= note: expected type `std::vec::Vec<i32>`
found type `&[i32]`
The suggested fix ref_items.clone().to_vec()
will work, but to_vec
already copies the data so the extra clone
is wasteful. The error message should instead suggest ref_items.to_vec()
.
This is an error you can run into if ref_items
was previously of type &Vec<i32>
and you changed it to a slice after writing the rest of the code.