Skip to content

hashset::contains() returns FALSE, although the item is in the set #218

Closed
@SvetlinZarev

Description

@SvetlinZarev

While playing AoC 2020 I've encountered a very strange issue with HashSet::contains() - it reports that an item is NOT present in the set, although it is. Here is a MCVE:

use std::collections::VecDeque;
//use std::collections::HashSet;

use hashbrown::HashSet;

pub fn main() {
    let mut deque = (0..10).collect::<VecDeque<usize>>();

    let mut seen_a = HashSet::new();
    let mut seen_b = HashSet::new();

    for i in 0..100 {
        if !seen_a.insert(deque.clone()) {
            if !seen_b.contains(&deque) {
                println!("{:3}: {:?}", i, deque);
            }
        }

        // already inserted
        // seen_a.insert(deque.clone());
        seen_b.insert(deque.clone());

        deque.rotate_left(1);
    }
}

Expected behavior:
The application does not print anything

Actual behavior
The application prints:

 10: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 11: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
 12: [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
 13: [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
 14: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
 15: [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
 23: [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
 24: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
 25: [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
 26: [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
 27: [7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
 28: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
 29: [9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
 30: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 31: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
 39: [9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
 40: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 41: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
 42: [2, 3, 4, 5, 6, 7, 8, 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, 2]
 94: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
 95: [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]

The issue is NOT reproducible with std::collections::HashSet. Just comment out the hashbrown import and uncomment the std one.

Hashbrown version: 0.9.1
rustc 1.48.0 (7eac88abb 2020-11-16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions