Writing an iterator

I am writing a Vector structure in Rust.

pub struct Vector {
    pub x: f32,
    pub y: f32,
    pub z: f32,

    curr: uint
}

And I would like to write a simple iterator for it so that I can iterate over the elements of the vector. This is sometimes useful, plus I know almost nothing about iterators in Rust.

Here is what I have at the moment.

impl Iterator<f32> for Vector {
    fn next(&mut self) -> Option<f32> {
        let new_next : Option<f32> = match self.curr {
            0 => Some(self.x),
            1 => Some(self.y),
            2 => Some(self.z), 
            _ => None
        };
        let new_curr = (self.curr + 1) % 4;
        mem::replace(&mut self.curr, new_curr);
        new_next
    }
}

Ideally, I would like to use this as:

let u = Vector::new(0.0f32, 0.0f32, 0.0f32);
for element in u {
    ///
}

However, I get the following compiler error:

 error: cannot borrow immutable local variable `u` as mutable

So I'm at a dead end. After a couple of hours in Google, I couldn't think of anything. I feel like I'm missing something huge.

+4
source share
2 answers

Do you really want to Vectorbe an iterator? Usually structures and iterators are separated in them. Consider something like this:

pub struct Vector {
    pub x: f32,
    pub y: f32,
    pub z: f32
}

pub struct VectorIter<'a> {
    vector: &'a Vector,
    cur: uint
}

impl<'a> Iterator<f32> for VectorIter<'a> {
    fn next(&mut self) -> Option<f32> {
        let r = match self.cur {
            0 => self.vector.x,
            1 => self.vector.y,
            2 => self.vector.z,
            _ => return None
        };
        self.cur += 1;
        Some(r)
    }
}

impl Vector {
    fn iter(&self) -> VectorIter {
        VectorIter {
            vector: self,
            cur: 0
        }
    }
}

let v = Vector { x: 1.0, y: 2.0, z: 3.0 };
for c in v.iter() {
    println!("{}", c);
}

, Vector , Copy, :

#[deriving(Copy)]
pub struct Vector {
    pub x: f32,
    pub y: f32,
    pub z: f32
}

#[deriving(Copy)]  // to silence the warning
pub struct VectorIter {
    vector: Vector,
    cur: uint
}

impl Iterator<f32> for VectorIter {
    fn next(&mut self) -> Option<f32> {
        let r = match self.cur {
            0 => self.vector.x,
            1 => self.vector.y,
            2 => self.vector.z,
            _ => return None
        };
        self.cur += 1;
        Some(r)
    }
}

impl Vector {
    fn iter(&self) -> VectorIter {
        VectorIter {
            vector: *self,
            cur: 0
        }
    }
}

let v = Vector { x: 1.0, y: 2.0, z: 3.0 };
for c in v.iter() {
    println!("{}", c);
}

, , , Vector , . , , , , ( Copy , , Copy ). , , .

+14

, . , .

error: u

-, , , . :

mem::replace(&mut self.curr, new_curr);

Rust , , next :

fn next(&mut self) -> Option<f32>

, :

let u = Vector::new(0.0f32, 0.0f32, 0.0f32);

let mut u = Vector::new(0.0f32, 0.0f32, 0.0f32);

, . , , , , . , !

+3

Source: https://habr.com/ru/post/1568604/


All Articles