pub struct HashSet<A, S = RandomState> { /* private fields */ }
Expand description

An unordered set.

An immutable hash set using [hash array mapped tries] 1.

Most operations on this set are O(logx n) for a suitably high x that it should be nearly O(1) for most sets. Because of this, it’s a great choice for a generic set as long as you don’t mind that values will need to implement Hash and Eq.

Values will have a predictable order based on the hasher being used. Unless otherwise specified, this will be the standard RandomState hasher.

Implementations

Construct an empty set.

Construct a set with a single value.

Examples
let set = HashSet::unit(123);
assert!(set.contains(&123));

Test whether a set is empty.

Time: O(1)

Examples
assert!(
  !hashset![1, 2, 3].is_empty()
);
assert!(
  HashSet::<i32>::new().is_empty()
);

Get the size of a set.

Time: O(1)

Examples
assert_eq!(3, hashset![1, 2, 3].len());

Test whether two sets refer to the same content in memory.

This is true if the two sides are references to the same set, or if the two sets refer to the same root node.

This would return true if you’re comparing a set to itself, or if you’re comparing a set to a fresh clone of itself.

Time: O(1)

Construct an empty hash set using the provided hasher.

Get a reference to the set’s BuildHasher.

Construct an empty hash set using the same hasher as the current hash set.

Discard all elements from the set.

This leaves you with an empty set, and all elements that were previously inside it are dropped.

Time: O(n)

Examples
let mut set = hashset![1, 2, 3];
set.clear();
assert!(set.is_empty());

Get an iterator over the values in a hash set.

Please note that the order is consistent between sets using the same hasher, but no other ordering guarantee is offered. Items will not come out in insertion order or sort order. They will, however, come out in the same order every time for the same set.

Test if a value is part of a set.

Time: O(log n)

Test whether a set is a subset of another set, meaning that all values in our set must also be in the other set.

Time: O(n log n)

Test whether a set is a proper subset of another set, meaning that all values in our set must also be in the other set. A proper subset must also be smaller than the other set.

Time: O(n log n)

Insert a value into a set.

Time: O(log n)

Remove a value from a set if it exists.

Time: O(log n)

Construct a new set from the current set with the given value added.

Time: O(log n)

Examples
let set = hashset![123];
assert_eq!(
  set.update(456),
  hashset![123, 456]
);

Construct a new set with the given value removed if it’s in the set.

Time: O(log n)

Filter out values from a set which don’t satisfy a predicate.

This is slightly more efficient than filtering using an iterator, in that it doesn’t need to rehash the retained values, but it still needs to reconstruct the entire tree structure of the set.

Time: O(n log n)

Examples
let mut set = hashset![1, 2, 3];
set.retain(|v| *v > 1);
let expected = hashset![2, 3];
assert_eq!(expected, set);

Construct the union of two sets.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 2, 3};
assert_eq!(expected, set1.union(set2));

Construct the union of multiple sets.

Time: O(n log n)

Construct the symmetric difference between two sets.

This is an alias for the symmetric_difference method.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 3};
assert_eq!(expected, set1.difference(set2));

Construct the symmetric difference between two sets.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 3};
assert_eq!(expected, set1.symmetric_difference(set2));

Construct the relative complement between two sets, that is the set of values in self that do not occur in other.

Time: O(m log n) where m is the size of the other set

Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{1};
assert_eq!(expected, set1.relative_complement(set2));

Construct the intersection of two sets.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{2};
assert_eq!(expected, set1.intersection(set2));

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Clone a set.

Time: O(1)

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.