How to dereference Uuid type?

Viewed 175

I'm using the Uuid crate to give unique ids to instantiate each new version of a Node struct with a unique identifier. Sometimes I'd like to filter these structs using .contains() to check if a struct's id is inside some array of Vec<Uuid>.

use uuid::Uuid; 

struct Node {
    id: Uuid,
}

impl Node {
    fn new() -> Self {
        let new_obj = Node {
            id: Uuid::new_v4()
        };
        new_obj
    }
    
    fn id(&self) -> Uuid {
        self.id
    }
}

fn main() {
    let my_objs = vec![
        Node::new(), 
        Node::new(), 
        Node::new(), 
        Node::new(), 
    ];
    let some_ids = vec![my_objs[0].id(), my_objs[3].id()];
}

fn filter_objs(all_items: &Vec<Node>, to_get: &Vec<Uuid>){
    for z in to_get {
        let wanted_objs = &all_items.iter().filter(|s| to_get.contains(*s.id()) == true);
    }
}

However this gives the error:

error[E0614]: type `Uuid` cannot be dereferenced
  --> src/main.rs:32:72
   |
32 |         let wanted_objs = &all_items.iter().filter(|s| to_get.contains(*s.id()) == true);
   |                                                                        ^^^^^^^

How can I enable dereferencing for the Uuid type to solve this problem?

Playground

1 Answers

Uuid doesn't implement the Deref trait so it can't be dereferenced, nor does it need to be since you're trying to pass it as an argument to a function with expects a reference. If you change *s.id() to &s.id() the code compiles:

fn filter_objs(all_items: &Vec<Node>, to_get: &Vec<Uuid>) {
    for z in to_get {
        let wanted_objs = &all_items
            .iter()
            // changed from `*s.id()` to `&s.id()` here
            .filter(|s| to_get.contains(&s.id()) == true);
    }
}

playground

Related