Uniqueness of array of references

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=11f769d6867c8e6743125b58ca04183e

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()));
5 Likes

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).

2 Likes

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.