pub struct OccupiedEntry<'a, K, T> { /* private fields */ }
Expand description
A view into an occupied entry in a LinkedHashMap
.
It is part of the Entry
enum.
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", "value");
if let Entry::Occupied(entry) = map.entry("key") {
println!("Found key: {}, value: {}", entry.key(), entry.get());
}
Implementations§
Source§impl<'a, K, T> OccupiedEntry<'a, K, T>
impl<'a, K, T> OccupiedEntry<'a, K, T>
Sourcepub fn get(&self) -> &T
pub fn get(&self) -> &T
Returns a reference to the value in the entry.
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(entry) => {
assert_eq!(entry.get(), &42);
}
Entry::Vacant(_) => unreachable!(),
}
Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the value in the entry.
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(mut entry) => {
*entry.get_mut() = 100;
}
Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.get(&"key"), Some(&100));
Sourcepub fn into_mut(self) -> &'a mut T
pub fn into_mut(self) -> &'a mut T
Consumes the occupied entry and returns a mutable reference to the value.
The returned reference is tied to the lifetime of the original map borrow.
Sourcepub fn insert_no_move(self, value: T) -> T
pub fn insert_no_move(self, value: T) -> T
Replaces the entry’s value and returns the old value without moving the entry’s position.
Unlike insert()
, this method does not affect the entry’s position in
the linked list.
§Arguments
value
- The new value to insert
§Returns
The previous value that was replaced
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("a", 1);
map.insert("b", 2);
match map.entry("a") {
Entry::Occupied(entry) => {
let old = entry.insert_no_move(10);
assert_eq!(old, 1);
}
Entry::Vacant(_) => unreachable!(),
}
// Order remains: a, b ("a" was not moved)
let entries: Vec<_> = map.iter().collect();
assert_eq!(entries, [(&"a", &10), (&"b", &2)]);
Sourcepub fn ptr(&self) -> Ptr
pub fn ptr(&self) -> Ptr
Returns the pointer to this entry.
The pointer can be used for direct access operations or cursor positioning.
§Returns
The pointer to the entry
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(entry) => {
let ptr = entry.ptr();
assert_eq!(map.ptr_get(ptr), Some(&42));
}
Entry::Vacant(_) => unreachable!(),
}
Sourcepub fn key(&self) -> &K
pub fn key(&self) -> &K
Returns a reference to the key in the entry.
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(entry) => {
assert_eq!(entry.key(), &"key");
}
Entry::Vacant(_) => unreachable!(),
}
Sourcepub fn insert(self, value: T) -> T
pub fn insert(self, value: T) -> T
Replaces the entry’s value and returns the old value.
This is equivalent to insert_no_move()
and does not move the entry’s
position.
§Arguments
value
- The new value to insert
§Returns
The previous value that was replaced
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(entry) => {
let old = entry.insert(100);
assert_eq!(old, 42);
}
Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.get(&"key"), Some(&100));
Sourcepub fn remove_entry(self) -> (K, T)
pub fn remove_entry(self) -> (K, T)
Removes the entry from the map and returns the key-value pair.
This consumes the occupied entry and requires that both the key and
value types implement Default
for safe removal from the underlying
storage.
§Returns
A tuple containing the key and value that were removed
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(entry) => {
let (key, value) = entry.remove_entry();
assert_eq!(key, "key");
assert_eq!(value, 42);
}
Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.len(), 0);
Sourcepub fn remove(self) -> T
pub fn remove(self) -> T
Removes the entry from the map and returns the value.
This consumes the occupied entry and requires that both the key and
value types implement Default
for safe removal from the underlying
storage.
§Returns
The value that was removed
§Examples
use tether_map::Entry;
use tether_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert("key", 42);
match map.entry("key") {
Entry::Occupied(entry) => {
let value = entry.remove();
assert_eq!(value, 42);
}
Entry::Vacant(_) => unreachable!(),
}
assert_eq!(map.len(), 0);