Struct OccupiedEntry

pub struct OccupiedEntry<'a, T, S, A: Allocator = Global> { pub(in ::collections::hash::set) base: OccupiedEntry<'a, T, S, A> }

A view into an occupied entry in a HashSet. It is part of the Entry enum.

Examples

#![feature(hash_set_entry)]

use std::collections::hash_set::{Entry, HashSet};

let mut set = HashSet::new();
set.extend(["a", "b", "c"]);

let _entry_o = set.entry("a").insert();
assert_eq!(set.len(), 3);

// Existing key
match set.entry("a") {
    Entry::Vacant(_) => unreachable!(),
    Entry::Occupied(view) => {
        assert_eq!(view.get(), &"a");
    }
}

assert_eq!(set.len(), 3);

// Existing key (take)
match set.entry("c") {
    Entry::Vacant(_) => unreachable!(),
    Entry::Occupied(view) => {
        assert_eq!(view.remove(), "c");
    }
}
assert_eq!(set.get(&"c"), None);
assert_eq!(set.len(), 2);

Fields

base: OccupiedEntry<'a, T, S, A>

Implementations

impl<T, S, A: Allocator> OccupiedEntry<'_, T, S, A>

fn get(&self) -> &T

Gets a reference to the value in the entry.

Examples

#![feature(hash_set_entry)]

use std::collections::hash_set::{Entry, HashSet};

let mut set = HashSet::new();
set.entry("poneyland").or_insert();

match set.entry("poneyland") {
    Entry::Vacant(_) => panic!(),
    Entry::Occupied(entry) => assert_eq!(entry.get(), &"poneyland"),
}
fn remove(self) -> T

Takes the value out of the entry, and returns it. Keeps the allocated memory for reuse.

Examples

#![feature(hash_set_entry)]

use std::collections::HashSet;
use std::collections::hash_set::Entry;

let mut set = HashSet::new();
// The set is empty
assert!(set.is_empty() && set.capacity() == 0);

set.entry("poneyland").or_insert();
let capacity_before_remove = set.capacity();

if let Entry::Occupied(o) = set.entry("poneyland") {
    assert_eq!(o.remove(), "poneyland");
}

assert_eq!(set.contains("poneyland"), false);
// Now set hold none elements but capacity is equal to the old one
assert!(set.len() == 0 && set.capacity() == capacity_before_remove);

Trait Implementations

impl<T: Debug, S, A: Allocator> Debug for OccupiedEntry<'_, T, S, A>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<'a, T, S, A = Global> !UnwindSafe for OccupiedEntry<'a, T, S, A>

impl<'a, T, S, A> Freeze for OccupiedEntry<'a, T, S, A> where OccupiedEntry<'a, T, S, A>: Freeze,

impl<'a, T, S, A> RefUnwindSafe for OccupiedEntry<'a, T, S, A> where OccupiedEntry<'a, T, S, A>: RefUnwindSafe,

impl<'a, T, S, A> Send for OccupiedEntry<'a, T, S, A> where OccupiedEntry<'a, T, S, A>: Send,

impl<'a, T, S, A> Sync for OccupiedEntry<'a, T, S, A> where OccupiedEntry<'a, T, S, A>: Sync,

impl<'a, T, S, A> Unpin for OccupiedEntry<'a, T, S, A> where OccupiedEntry<'a, T, S, A>: Unpin,

impl<'a, T, S, A> UnsafeUnpin for OccupiedEntry<'a, T, S, A> where OccupiedEntry<'a, T, S, A>: UnsafeUnpin,

Blanket Implementations

impl<T> Any for OccupiedEntry<'a, T, S, A> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for OccupiedEntry<'a, T, S, A> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for OccupiedEntry<'a, T, S, A> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for OccupiedEntry<'a, T, S, A>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for OccupiedEntry<'a, T, S, A> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for OccupiedEntry<'a, T, S, A>

impl<T, U> Into<U> for OccupiedEntry<'a, T, S, A> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for OccupiedEntry<'a, T, S, A> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for OccupiedEntry<'a, T, S, A> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>