#search field of the struct in the Vec and get struct

3 messages · Page 1 of 1 (latest)

umbral storm
#

Hi. I have a struct called Cell and find() function inside of the Game.
Game struct has Vec<Cell> as it's field and find() function searches the position of the cell and return Option<Cell> as a cell might not be found.
I'm not really familiar with vector handling and so Im not so sure what i am doing right now.
I appreciate any help/advice :)

cell.rs

#[derive(PartialEq)]
pub struct Cell {
    pub pos: (f32, f32),
}

impl FromIterator<Cell> for Cell {
    fn collect<I: IntoIterator<Item = Cell>>(iter: I) -> Self {
                 //I'm not sure what im doing
        }
}

game.rs

pub fn find(&mut self, x: f32, y: f32) -> Option<Cell> {
    let cell: Cell = self
        .cells
        .into_iter()
        .filter(|&a| a.pos == (x, y))
        .collect(); // here

    if (x, y) == cell.pos {
        Some(cell)
    } else {
        None
    }
}
covert gull
#

Use the Iterator::find method,

#

It's what Iterator::filter ends up using, just for a single item.