pub struct Set<T: Eq + Hash = Key> { /* private fields */ }
Expand description
A hash set implemented as a Map
where the value is ()
.
Implementations§
Source§impl<T: Eq + Hash> Set<T>
impl<T: Eq + Hash> Set<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new empty Set
, with specified capacity.
§Example
let mut set = Set::with_capacity(2);
set.insert("x");
set.insert("y");
// The next insert will likely require reallocation...
set.insert("z");
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the set can hold without reallocating.
§Example
let set = Set::<Key>::with_capacity(2);
assert!(set.capacity() >= 2);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all elements. Keeps the allocated memory for reuse.
§Example
let mut set = Set::new();
set.insert("x");
set.clear();
assert!(set.is_empty());
Sourcepub fn contains<Q>(&self, key: &Q) -> bool
pub fn contains<Q>(&self, key: &Q) -> bool
Returns true if the set contains the specified value.
§Example
let mut set = Set::new();
set.insert(1);
assert!(set.contains(&1));
assert!(!set.contains(&2));
Sourcepub fn drain(&mut self, range: RangeFull) -> Drain<'_, T> ⓘ
pub fn drain(&mut self, range: RangeFull) -> Drain<'_, T> ⓘ
Clears the set, returning all elements in an iterator. Keeps the allocated memory for reuse.
§Example
let mut set = Set::new();
set.insert(1);
set.insert(2);
for item in set.drain(..) {
assert!(item == 1 || item == 2);
}
assert!(set.is_empty());
Sourcepub fn insert(&mut self, key: T) -> bool
pub fn insert(&mut self, key: T) -> bool
Adds a value to the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
§Example
let mut set = Set::new();
assert_eq!(set.insert(1), true);
assert_eq!(set.insert(1), false);
assert_eq!(set.len(), 1);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set does not contain any elements.
§Example
let mut set = Set::new();
assert!(set.is_empty());
set.insert("x");
assert!(!set.is_empty());
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Return an iterator visiting all the elements of the set in the order in which they were inserted.
§Example
let mut set = Set::new();
set.insert("a");
set.insert("b");
set.insert("c");
let mut iter = set.iter();
assert_eq!(iter.next(), Some(&"a"));
assert_eq!(iter.next(), Some(&"b"));
assert_eq!(iter.next(), Some(&"c"));
assert_eq!(iter.next(), None);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of elements in the set.
§Example
let mut set = Set::new();
assert_eq!(set.len(), 0);
set.insert("x");
assert_eq!(set.len(), 1);
Sourcepub fn remove<Q>(&mut self, key: &Q) -> bool
pub fn remove<Q>(&mut self, key: &Q) -> bool
Removes a value from the set. Returns true
if the value was present
in the set.
§Example
let mut set = Set::new();
set.insert("x");
assert!(set.remove("x"));
assert!(!set.remove("x"));
assert_eq!(set.len(), 0);
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Set<T>
impl<'de, T> Deserialize<'de> for Set<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T: Eq + Hash> Extend<T> for Set<T>
impl<T: Eq + Hash> Extend<T> for Set<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T: Eq + Hash> FromIterator<T> for Set<T>
impl<T: Eq + Hash> FromIterator<T> for Set<T>
Source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = T>,
impl<T: Eq + Eq + Hash> Eq for Set<T>
impl<T: Eq + Hash> StructuralPartialEq for Set<T>
Auto Trait Implementations§
impl<T> Freeze for Set<T>
impl<T> RefUnwindSafe for Set<T>where
T: RefUnwindSafe,
impl<T> Send for Set<T>where
T: Send,
impl<T> Sync for Set<T>where
T: Sync,
impl<T> Unpin for Set<T>where
T: Unpin,
impl<T> UnwindSafe for Set<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.