let assume that we have a vector of points written as a flat sequence of integers. size of vector is precise and we want to convert it to vector of Point structs. So my conversion currently looks like this.
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new(x: i32, y : i32) -> Point {
Point{x,y}
}
}
impl From<Point> for String {
fn from(point: Point) -> String {
format!("({},{})", point.x, point.y)
}
}
fn main() {
let vec = vec![2,1,4,3,6,5]; // We want convert that
let even = vec.iter().step_by(2);
let odd = vec.iter().skip(1).step_by(2);
let points: Vec<Point>
= even.zip(odd)
.map(|(x,y)|Point::new(*x,*y))
.collect();
println!("{:?}", points);
}
Works like a charm, until we want to add z component, and peek into three elements per cycle. I haven't found any appropriate methods from docs. So, is there already a generic way to iterate over slices of some size over vector and unpack it into my struct(s) or there's no such way and I should implement such iterator myself? Is it a signal to use some heavier machinery like serde.