VacantEntry in std::collections::btree_set - Rust (original) (raw)

pub struct VacantEntry<'a, T, A = Global>

where
    A: Allocator + Clone,

{ /* private fields */ }

🔬This is a nightly-only experimental API. (btree_set_entry #133549)

Expand description

A view into a vacant entry in a BTreeSet. It is part of the Entry enum.

§Examples

#![feature(btree_set_entry)]

use std::collections::btree_set::{Entry, BTreeSet};

let mut set = BTreeSet::<&str>::new();

let entry_v = match set.entry("a") {
    Entry::Vacant(view) => view,
    Entry::Occupied(_) => unreachable!(),
};
entry_v.insert();
assert!(set.contains("a") && set.len() == 1);

// Nonexistent key (insert)
match set.entry("b") {
    Entry::Vacant(view) => view.insert(),
    Entry::Occupied(_) => unreachable!(),
}
assert!(set.contains("b") && set.len() == 2);

Source§

Source

🔬This is a nightly-only experimental API. (btree_set_entry #133549)

Gets a reference to the value that would be used when inserting through the VacantEntry.

§Examples
#![feature(btree_set_entry)]

use std::collections::BTreeSet;

let mut set = BTreeSet::new();
assert_eq!(set.entry("poneyland").get(), &"poneyland");

Source

🔬This is a nightly-only experimental API. (btree_set_entry #133549)

Take ownership of the value.

§Examples
#![feature(btree_set_entry)]

use std::collections::btree_set::{Entry, BTreeSet};

let mut set = BTreeSet::new();

match set.entry("poneyland") {
    Entry::Occupied(_) => panic!(),
    Entry::Vacant(v) => assert_eq!(v.into_value(), "poneyland"),
}

Source

🔬This is a nightly-only experimental API. (btree_set_entry #133549)

Sets the value of the entry with the VacantEntry’s value.

§Examples
#![feature(btree_set_entry)]

use std::collections::BTreeSet;
use std::collections::btree_set::Entry;

let mut set = BTreeSet::new();

if let Entry::Vacant(o) = set.entry("poneyland") {
    o.insert();
}
assert!(set.contains("poneyland"));