btree_set
import std::collections::btree_set; · source
Types
BTreeSet<T, A>
type struct BTreeSet<T, A = GlobalAllocator> where A: Allocator {
private map: BTreeMap<T, (), A>;
}
A set of distinct values kept in ascending order: a BTreeMap whose values are (). Values need Ord; finding, inserting, and removing are O(log n), and iteration runs in ascending order from either end.
import std::collections::btree_set::{ BTreeSet };
mut seen: BTreeSet<i64> = BTreeSet::new();
seen.insert(3);
seen.insert(1);
const added: boolean = seen.insert(3); // false: already present
const smallest: Option<i64*> = seen.first(); // 1
insert returns true when the value was not already present — a value already there is kept, and the new one released — and remove returns true when it was present. first and last borrow the smallest and greatest values; pop_first and pop_last take them out. iter() yields by value and needs T: Copy; iterate_references() and range(bounds) borrow. Every iterator is a DoubleEndedIterator.
Constructors
static new() -> BTreeSet<T, GlobalAllocator>;
static new() -> BTreeSet<T, GlobalAllocator>;An empty set backed by GlobalAllocator. Allocates nothing until the first insert.
static new_in(alloc: A) -> BTreeSet<T, A>;
static new_in(alloc: A) -> BTreeSet<T, A>;An empty set that allocates through alloc.
Methods
length(&this) -> i64;
length(&this) -> i64;The number of values in the set.
is_empty(&this) -> boolean;
is_empty(&this) -> boolean;True when the set holds no values.
contains(&this, value: &T) -> boolean where T: Ord;
contains(&this, value: &T) -> boolean where T: Ord;True when value is a member.
insert(mut &this, value: T) -> boolean where T: Ord;
insert(mut &this, value: T) -> boolean where T: Ord;Add value. Returns true if it was not already present; a value already present is kept and value is released. Panics on allocation failure.
try_insert(mut &this, value: T) -> Result<boolean, AllocationError> where T: Ord;
try_insert(mut &this, value: T) -> Result<boolean, AllocationError> where T: Ord;insert that returns Err(AllocationError) instead of panicking when a node cannot be allocated.
remove(mut &this, value: &T) -> boolean where T: Ord;
remove(mut &this, value: &T) -> boolean where T: Ord;Remove value. Returns true if it was present.
first(&this) -> Option<T*>;
first(&this) -> Option<T*>;The smallest value, or None when empty.
last(&this) -> Option<T*>;
last(&this) -> Option<T*>;The greatest value, or None when empty.
pop_first(mut &this) -> Option<T> where T: Ord;
pop_first(mut &this) -> Option<T> where T: Ord;Remove and return the smallest value, or None when empty.
pop_last(mut &this) -> Option<T> where T: Ord;
pop_last(mut &this) -> Option<T> where T: Ord;Remove and return the greatest value, or None when empty.
clear(mut &this) -> void;
clear(mut &this) -> void;Release every value.
iter(&this) -> implement Iterator<T> + DoubleEndedIterator where T: Copy;
iter(&this) -> implement Iterator<T> + DoubleEndedIterator where T: Copy;Every value by value, in ascending order. Gated on T: Copy: the values are bit copies of the stored ones. iterate_references walks any set.
iterate_references(&this) -> implement Iterator<T*> + DoubleEndedIterator;
iterate_references(&this) -> implement Iterator<T*> + DoubleEndedIterator;Every value by borrow, in ascending order.
range<R>(&this, bounds: R) -> implement Iterator<T*> + DoubleEndedIterator where T: Ord, R: RangeBounds<T>;
range<R>(&this, bounds: R) -> implement Iterator<T*> + DoubleEndedIterator where T: Ord, R: RangeBounds<T>;The values in bounds, by borrow, in ascending order and from either end: set.range(low..high), set.range(low..=high), set.range(..high), set.range(..), with bounds of the value type.
Trait implementations
implement<T> trait Clone for struct BTreeSet<T, GlobalAllocator> where T: Ord + Clone
implement<T> trait BitOr<BTreeSet<T, GlobalAllocator>, BTreeSet<T, GlobalAllocator>> for struct BTreeSet<T, GlobalAllocator> where T: Ord + Clone
implement<T> trait BitAnd<BTreeSet<T, GlobalAllocator>, BTreeSet<T, GlobalAllocator>> for struct BTreeSet<T, GlobalAllocator> where T: Ord + Clone
implement<T> trait Sub<BTreeSet<T, GlobalAllocator>, BTreeSet<T, GlobalAllocator>> for struct BTreeSet<T, GlobalAllocator> where T: Ord + Clone
implement<T> trait BitXor<BTreeSet<T, GlobalAllocator>, BTreeSet<T, GlobalAllocator>> for struct BTreeSet<T, GlobalAllocator> where T: Ord + Clone
implement<T> trait Default for struct BTreeSet<T, GlobalAllocator>
implement<T> trait FromIterator<T> for struct BTreeSet<T, GlobalAllocator> where T: Ord
implement<T, A> trait Iterable for struct BTreeSet<T, A> where T: Copy, A: Allocator
implement<T, A> trait Display for struct BTreeSet<T, A> where T: Display, A: Allocator
std::fmt::display
implement<T, A> trait Debug for struct BTreeSet<T, A> where T: Debug, A: Allocator
std::fmt::display
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct BTreeSet<T, A = GlobalAllocator> where A: Allocator {}A set of distinct values kept in ascending order: a BTreeMap whose values are (). Values need Ord.
type parameter Ttype parameter Awhere A: Allocator
type struct GlobalAllocator {}Zero-sized handle to the process-wide allocator. Construct with GlobalAllocator::new(); all instances dispatch through the same underlying backend.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait Allocator {
allocate(mut &this, layout: Layout) -> Result<NonNull<u8>, AllocationError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocationError>;
}A source of heap memory: hands out blocks described by a Layout and takes them back. Collections, Box and String take an A: Allocator type parameter, so the storage strategy is the caller's choice; GlobalAllocator is the default.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
BTreeSet.map: BTreeMap<T, (), A>type struct BTreeMap<K, V, A = GlobalAllocator> where A: Allocator {}A map from keys to values kept in ascending key order. Keys need Ord. Lookups and inserts take time logarithmic in the size; iteration and range visit keys in order.
type parameter Tprimitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::collections::btree_set;BTreeSet<T, A>: a set kept in order, built on BTreeMap<T, ()>.
type struct BTreeSet<T, A = GlobalAllocator> where A: Allocator {}A set of distinct values kept in ascending order: a BTreeMap whose values are (). Values need Ord.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut seen: BTreeSet<i64>static BTreeSet::new() -> BTreeSet<T, GlobalAllocator>An empty set backed by GlobalAllocator. Allocates nothing until the first insert.
mut seen: BTreeSet<i64>BTreeSet::insert(mut &this, value: T) -> boolean
where T: OrdAdd value. Returns true if it was not already present; a value already present is kept and value is released. Panics on allocation failure.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const added: booleanconst smallest: Option<i64*>type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
BTreeSet::first(&this) -> Option<T*>The smallest value, or None when empty.
type trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
trueThe boolean literal. Booleans are a distinct 1-byte type, not interchangeable with integers; if and while conditions must be boolean.
BTreeSet::remove(mut &this, value: &T) -> boolean
where T: OrdRemove value. Returns true if it was present.
BTreeSet::last(&this) -> Option<T*>The greatest value, or None when empty.
BTreeSet::pop_first(mut &this) -> Option<T>
where T: OrdRemove and return the smallest value, or None when empty.
BTreeSet::pop_last(mut &this) -> Option<T>
where T: OrdRemove and return the greatest value, or None when empty.
BTreeSet::iter(&this) -> implement Iterator<T> + DoubleEndedIterator
where T: CopyEvery value by value, in ascending order. Gated on T: Copy: the values are bit copies of the stored ones. iterate_references walks any set.
type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markerBTreeSet::iterate_references(&this) -> implement Iterator<T*> + DoubleEndedIteratorEvery value by borrow, in ascending order.
std::collections::btree_setBTreeSet::range<R>(&this, bounds: R) -> implement Iterator<T*> + DoubleEndedIterator
where T: Ord, R: RangeBounds<T>The values in bounds, by borrow, in ascending order and from either end: set.range(low..high), set.range(low..=high), set.range(..high), set.range(..), with bounds of the value type.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static BTreeSet::new_in(alloc: A) -> BTreeSet<T, A>An empty set that allocates through alloc.
(parameter) alloc: A&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
BTreeSet::contains(&this, value: &T) -> boolean
where T: OrdTrue when value is a member.
(parameter) value: &Tmut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
(parameter) value: TBTreeSet::try_insert(mut &this, value: T) -> Result<boolean, AllocationError>
where T: Ordinsert that returns Err(AllocationError) instead of panicking when a node cannot be allocated.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct AllocationError {
kind: AllocationErrorKind;
layout: Layout;
}The error an Allocator returns: what went wrong (kind) and the layout that was requested when it did (layout; for a reallocate, the new layout).
Result::Err(E)Option::None-> implement Trait<T>An opaque type: some one concrete type that implements the trait, without naming it. The compiler infers the real type and uses it everywhere; callers see only the trait's methods. How the stdlib returns iterators without exposing their engines.
language referencetype trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> i64;
fold<Acc, F>(mut &this, initial: Acc, f: F) -> Acc
where F: (Acc, This::Item) -> Acc;
for_each<F>(mut &this, f: F) -> void
where F: (This::Item) -> void;
take(this, n: i64) -> implement Iterator<This::Item>;
skip(this, n: i64) -> implement Iterator<This::Item>;
map<B, F>(this, f: F) -> implement Iterator<B>
where F: (This::Item) -> B;
filter<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
chain<J>(this, other: J) -> implement Iterator<This::Item>
where J: Iterator<This::Item>;
enumerate(this) -> implement Iterator<(i64, This::Item)>;
zip<J>(this, other: J) -> implement Iterator<(This::Item, J::Item)>
where J: Iterator;
collect<C>(this) -> C
where C: FromIterator<This::Item>;
sum<S>(this) -> S
where S: Sum, This: Iterator<S>;
reverse(this) -> implement Iterator<This::Item>
where This: DoubleEndedIterator;
peekable(this) -> implement Peekable<This::Item>;
flat_map<B, F>(this, f: F) -> implement Iterator<B::Item>
where F: (This::Item) -> B, B: Iterator;
flatten(this) -> implement Iterator<This::Item::Item>
where This::Item: Iterator;
take_while<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
skip_while<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
step_by(this, step: i64) -> implement Iterator<This::Item>;
filter_map<B, F>(this, f: F) -> implement Iterator<B>
where F: (This::Item) -> Option<B>;
any<P>(mut &this, pred: P) -> boolean
where P: (This::Item) -> boolean;
all<P>(mut &this, pred: P) -> boolean
where P: (This::Item) -> boolean;
find<P>(mut &this, pred: P) -> Option<This::Item>
where P: (&This::Item) -> boolean;
min(mut &this) -> Option<This::Item>
where This::Item: Ord;
max(mut &this) -> Option<This::Item>
where This::Item: Ord;
min_by<F>(mut &this, compare: F) -> Option<This::Item>
where F: (&This::Item, &This::Item) -> Ordering;
max_by<F>(mut &this, compare: F) -> Option<This::Item>
where F: (&This::Item, &This::Item) -> Ordering;
min_by_key<K, F>(mut &this, key: F) -> Option<This::Item>
where F: (&This::Item) -> K, K: Ord;
max_by_key<K, F>(mut &this, key: F) -> Option<This::Item>
where F: (&This::Item) -> K, K: Ord;
last(mut &this) -> Option<This::Item>;
nth(mut &this, n: i64) -> Option<This::Item>;
position<P>(mut &this, pred: P) -> Option<i64>
where P: (This::Item) -> boolean;
product<S>(this) -> S
where S: Product, This: Iterator<S>;
}A source of values handed out one at a time by next, until it returns None. for (x in source) drives one, and the adapter methods below build new iterators from it:
type trait DoubleEndedIterator : Iterator {
next_back(mut &this) -> Option<This::Item>;
}An iterator that also yields from its back end. next and next_back draw from one sequence, so between them each element is yielded once.
(parameter) bounds: Rtype trait RangeBounds<T> {
start_bound(&this) -> Bound<&T>;
end_bound(&this) -> Bound<&T>;
}A range over an ordered T, whichever range form wrote it: a..b, a..=b, a.., ..b or ... A method that takes a range of keys (BTreeMap.range) takes any of them through this trait. The full range .. names no T and is a range of every T: a call through the bound takes T from the bound's argument.
start..endA half-open range - start up to but excluding end - and sugar for Range::new(start, end): an iterator usable in for, and an ordinary value anywhere else. It binds looser than arithmetic, so a..b + 1 is a..(b + 1).
start..=endAn inclusive range - start through end - and sugar for RangeInclusive::new(start, end). Also a match pattern: 1..=5 => { ... }.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type parameter Twhere T: Ord + Clone
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Clone {
clone(&this) -> This;
}A type whose values can be duplicated into independent copies. A type takes it with with [Clone] on its declaration, or by implementing clone by hand.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait BitOr<Rhs, Output> {
bitor(&this, rhs: &Rhs) -> Output;
}Bitwise or, a | b => a.bitor(&b); see BitAnd for how the operator reaches it.
type trait BitAnd<Rhs, Output> {
bitand(&this, rhs: &Rhs) -> Output;
}Bitwise and shift operator traits, rewritten exactly like the arithmetic ones: a & b => a.bitand(&b), and likewise | => bitor, ^ => bitxor, << => shl, >> => shr.
type trait Sub<Rhs, Output> {
sub(&this, rhs: &Rhs) -> Output;
}Subtraction, a - b => a.sub(&b); see Add for how the operator reaches it.
type trait BitXor<Rhs, Output> {
bitxor(&this, rhs: &Rhs) -> Output;
}Bitwise exclusive or, a ^ b => a.bitxor(&b); see BitAnd for how the operator reaches it.
type trait Default {
static default() -> This;
}A type with a canonical starting value, which generic code can make without knowing the type: 0 for the numbers, false, '\0', and an empty value for the collections that implement it.
type parameter Twhere T: Ord
type trait FromIterator<T> {
static from_iterator<I>(source: I) -> This
where I: Iterator<T>;
}A collection built from the elements of an iterator: what collect builds.
type parameter Twhere T: Copy
type trait Iterable {
type Iteration;
iterate(this) -> This::Iteration;
}A value a for loop can loop over that is not itself an Iterator: the loop consumes it, iterate turns it into the iterator the loop drives, and each element is handed to the body as a value of its own.
type parameter Twhere T: Display
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
type parameter Twhere T: Debug
type trait Debug {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
}How a value reads to a programmer: what {x:?} writes, and {x:#?} over several lines. with [Debug] on a declaration writes it field by field.