Code/Recipes/OrderedVecMap

From Polytech Center Barcelona
Jump to: navigation, search
type Id = u32;

#[derive(Clone)]
struct Node {
    key: Id
}

struct App {
    nodes: Vec<Node>
}

impl App {
    fn lookup(&self, key: &Id) -> Option<Node> {
        self.nodes.iter().find(|&x| x.key == *key).cloned()
    }
}

fn main() {
    let app = App { nodes: vec![Node { key: 110 }, Node { key: 4 }] };
    app.lookup(&110).expect("node 110 not found!");
    // this will panic
    app.lookup(&7).expect("node 7 not found!");
}