pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(K, &mut V) -> bool,
    {
        for i in 1..self.slots.len() {
            // This is safe because removing elements does not shrink slots.
            let slot = unsafe { self.slots.get_unchecked_mut(i) };
            let version = slot.version;

            let should_remove = if let OccupiedMut(value) = slot.get_mut() {
                let key = KeyData::new(i as u32, version).into();
                !f(key, value)
            } else {
                false
            };

            if should_remove {
                // This is safe because we know that the slot was occupied.
                unsafe { self.remove_from_slot(i) };
            }
        }
    }
/// Retains only the elements specified by the predicate.
///
/// In other words, remove all key-value pairs `(k, v)` such that
/// `f(k, &mut v)` returns false. This method invalidates any removed keys.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
///
/// # Examples
///
/// ```
/// # use slotmap::*;
/// let mut sm = SlotMap::new();
///
/// let k1 = sm.insert(0);
/// let k2 = sm.insert(1);
/// let k3 = sm.insert(2);
///
/// sm.retain(|key, val| key == k1 || *val == 1);
///
/// assert!(sm.contains_key(k1));
/// assert!(sm.contains_key(k2));
/// assert!(!sm.contains_key(k3));
///
/// assert_eq!(2, sm.len());
/// ```
Retains only the elements specified by the predicate.
In other words, remove all key-value pairs `(k, v)` such that
`f(k, &mut v)` returns false. This method invalidates any removed keys.

This function must iterate over all slots, empty or not. In the face of
many deleted elements it can be inefficient.
Retains only the elements specified by the predicate . In other words remove all key - value pairs ` ( k v ) ` such that ` f ( k &mut v ) ` returns false . This method invalidates any removed keys . This function must iterate over all slots empty or not . In the face of many deleted elements it can be inefficient .
pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(K, &mut V) -> bool,
    {
        for i in 1..self.slots.len() {
            // This is safe because removing elements does not shrink slots.
            let slot = unsafe { self.slots.get_unchecked_mut(i) };
            let version = slot.version;
            let should_remove = if let OccupiedMut(value) = slot.get_mut() {
                let key = KeyData::new(i as u32, version).into();
                !f(key, value)
            } else {
                false
            };
            if should_remove {
                // This is safe because we know that the slot was occupied.
                unsafe { self.remove_from_slot(i) };
            }
        }
    }
pub fn retain < F > ( & mut self , mut f : F ) where F : FnMut ( K , & mut V ) -> bool , { for i in 1 .. self . slots . len ( ) { // This is safe because removing elements does not shrink slots. let slot = unsafe { self . slots . get_unchecked_mut ( i ) } ; let version = slot . version ; let should_remove = if let OccupiedMut ( value ) = slot . get_mut ( ) { let key = KeyData :: new ( i as u32 , version ) . into ( ) ; ! f ( key , value ) } else { false } ; if should_remove { // This is safe because we know that the slot was occupied. unsafe { self . remove_from_slot ( i ) } ; } } }
Retains only the elements specified by the predicate.
Retains only the elements specified by the predicate .
&mut self
f
F
examples
None
use slotmap::*;
let mut sm = SlotMap::new().








let mut sm = SlotMap :: new () .