Uniqueness of array of references


Can't have a HashSet of Rc<RefCell>. I understand why. But I need to at least be able to eliminate duplicates from an array of such things. Any good ideas? Safe Rust only, please.

I know that you can't compare references easily. Why is that? Religious issue from the functional crowd?

You can put it in a wrapper struct and implement Hash and Eq in an appropriate manner.

Some relevant answers from a previous thread:

If you just need a one-time elimination:

    let mut set = vec![one, two, three];
    let mut seen = HashSet::new();
    set.retain(|item| seen.insert(item.as_ptr()));

Probably because this impl means trying to compare two &T's will result in dereferencing and comparing the underlying T's. So comparing references with == means you'll always get value equality and not reference equality.

I think that has a big flow-on effect, and contributes to why Rust cares so much more about whether two things are equivalent (value equality) rather than being the same object (reference equality).


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.