I have a Vec
of nontrivial types with a size I am certain of. I need to convert this into fixed size array. Ideally I would like to do this
- without copying the data over - I would like to consume the
Vec
- without preinitializing the array with zero data because that would be a waste of CPU cycles
Question written as code:
struct Point {
x: i32,
y: i32,
}
fn main() {
let points = vec![
Point { x: 1, y: 2 },
Point { x: 3, y: 4 },
Point { x: 5, y: 6 },
];
// I would like this to be an array of points
let array: [Point; 3] = ???;
}
This seems like a trivial issue, however I have not been able to find satisfactory solution in Vec
docs, slicing sections of Rust Books or by Googling. Only thing that I found is to first initialize the array with zero data and later copy all elements over from Vec
, however this does not satisfy my requirements.