Enum EntryRef
pub enum EntryRef<'a, 'b, K, Q: ?Sized, V, S, A = Global>
where
A: Allocator,
A view into a single entry in a map, which may either be vacant or occupied, with any borrowed form of the map's key type.
This enum is constructed from the entry_ref method on HashMap.
Hash and Eq on the borrowed form of the map's key type must match those
for the key type. It also require that key may be constructed from the borrowed
form through the ToOwned trait.
Examples
use ;
let mut map = new;
map.extend;
assert_eq!;
// Existing key (insert)
let key = Stringfrom;
let entry: = map.entry_ref;
let _raw_o: = entry.insert;
assert_eq!;
// Nonexistent key (insert)
map.entry_ref.insert;
// Existing key (or_insert)
let v = map.entry_ref.or_insert;
assert_eq!;
// Nonexistent key (or_insert)
map.entry_ref.or_insert;
// Existing key (or_insert_with)
let v = map.entry_ref.or_insert_with;
assert_eq!;
// Nonexistent key (or_insert_with)
map.entry_ref.or_insert_with;
println!;
for in .into_iter.zip
assert_eq!;
Variants
-
Occupied(OccupiedEntry<'a, K, V, S, A>) An occupied entry.
Examples
use ; let mut map: = .into; match map.entry_ref-
Vacant(VacantEntryRef<'a, 'b, K, Q, V, S, A>) A vacant entry.
Examples
use ; let mut map: = new; match map.entry_ref
Implementations
impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A>
fn insert(self, value: V) -> OccupiedEntry<'a, K, V, S, A> where K: Hash, Q: ToOwned<Owned = K>, S: BuildHasher,Sets the value of the entry, and returns an
OccupiedEntry.Examples
use HashMap; let mut map: = new; let entry = map.entry_ref.insert; assert_eq!;fn or_insert(self, default: V) -> &'a mut V where K: Hash, Q: ToOwned<Owned = K>, S: BuildHasher,Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
Examples
use HashMap; let mut map: = new; // nonexistent key map.entry_ref.or_insert; assert_eq!; // existing key *map.entry_ref.or_insert *= 2; assert_eq!;fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V where K: Hash, Q: ToOwned<Owned = K>, S: BuildHasher,Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
Examples
use HashMap; let mut map: = new; // nonexistent key map.entry_ref.or_insert_with; assert_eq!; // existing key *map.entry_ref.or_insert_with *= 2; assert_eq!;fn or_insert_with_key<F: FnOnce(&Q) -> V>(self, default: F) -> &'a mut V where K: Hash + Borrow<Q>, Q: ToOwned<Owned = K>, S: BuildHasher,Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows for generating key-derived values for insertion by providing the default function an access to the borrower form of the key.
Examples
use HashMap; let mut map: = new; // nonexistent key map.entry_ref.or_insert_with_key; assert_eq!; // existing key *map.entry_ref.or_insert_with_key *= 2; assert_eq!;fn key(&self) -> &Q where K: Borrow<Q>,Returns a reference to this entry's key.
Examples
use HashMap; let mut map: = new; map.entry_ref.or_insert; // existing key assert_eq!; // nonexistent key assert_eq!;fn and_modify<F>(self, f: F) -> Self where F: FnOnce(&mut V),Provides in-place mutable access to an occupied entry before any potential inserts into the map.
Examples
use HashMap; let mut map: = new; map.entry_ref .and_modify .or_insert; assert_eq!; map.entry_ref .and_modify .or_insert; assert_eq!;fn into_map(self) -> &'a mut HashMap<K, V, S, A>Converts the
EntryRefinto a mutable reference to the underlying map.
impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A>
fn or_default(self) -> &'a mut V where K: Hash, Q: ToOwned<Owned = K>, S: BuildHasher,Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.
Examples
use HashMap; let mut map: = new; // nonexistent key map.entry_ref.or_default; assert_eq!; map.insert; // existing key assert_eq!;fn or_default_entry(self) -> OccupiedEntry<'a, K, V, S, A> where K: Hash, Q: ToOwned<Owned = K>, S: BuildHasher,Ensures a value is in the entry by inserting the default value if empty, and returns an
OccupiedEntry.Examples
use HashMap; let mut map: = new; // nonexistent key let entry = map.entry_ref.or_default_entry; assert_eq!; assert_eq!; // existing key map.insert; let entry = map.entry_ref.or_default_entry; assert_eq!; assert_eq!;
Trait Implementations
impl<K, Q, V, S, A> Debug for EntryRef<'_, '_, K, Q, V, S, A>
where
K: Debug + Borrow<Q>,
Q: Debug + ?Sized,
V: Debug,
A: Allocator,
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Auto Trait Implementations
impl<'a, 'b, K, Q, V, S, A = Global> !UnwindSafe for EntryRef<'a, 'b, K, Q, V, S, A>
impl<'a, 'b, K, Q, V, S, A> Freeze for EntryRef<'a, 'b, K, Q, V, S, A>
where
OccupiedEntry<'a, K, V, S, A>: Freeze,
VacantEntryRef<'a, 'b, K, Q, V, S, A>: Freeze,
Q: ?Sized,
impl<'a, 'b, K, Q, V, S, A> RefUnwindSafe for EntryRef<'a, 'b, K, Q, V, S, A>
where
OccupiedEntry<'a, K, V, S, A>: RefUnwindSafe,
VacantEntryRef<'a, 'b, K, Q, V, S, A>: RefUnwindSafe,
Q: ?Sized,
impl<'a, 'b, K, Q, V, S, A> Send for EntryRef<'a, 'b, K, Q, V, S, A>
where
OccupiedEntry<'a, K, V, S, A>: Send,
VacantEntryRef<'a, 'b, K, Q, V, S, A>: Send,
Q: ?Sized,
impl<'a, 'b, K, Q, V, S, A> Sync for EntryRef<'a, 'b, K, Q, V, S, A>
where
OccupiedEntry<'a, K, V, S, A>: Sync,
VacantEntryRef<'a, 'b, K, Q, V, S, A>: Sync,
Q: ?Sized,
impl<'a, 'b, K, Q, V, S, A> Unpin for EntryRef<'a, 'b, K, Q, V, S, A>
where
OccupiedEntry<'a, K, V, S, A>: Unpin,
VacantEntryRef<'a, 'b, K, Q, V, S, A>: Unpin,
Q: ?Sized,
impl<'a, 'b, K, Q, V, S, A> UnsafeUnpin for EntryRef<'a, 'b, K, Q, V, S, A>
where
OccupiedEntry<'a, K, V, S, A>: UnsafeUnpin,
VacantEntryRef<'a, 'b, K, Q, V, S, A>: UnsafeUnpin,
Q: ?Sized,
Blanket Implementations
impl<T> Any for EntryRef<'a, 'b, K, Q, V, S, A>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for EntryRef<'a, 'b, K, Q, V, S, A>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for EntryRef<'a, 'b, K, Q, V, S, A>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for EntryRef<'a, 'b, K, Q, V, S, A>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for EntryRef<'a, 'b, K, Q, V, S, A>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for EntryRef<'a, 'b, K, Q, V, S, A>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for EntryRef<'a, 'b, K, Q, V, S, A>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>