deque
import std::collections::deque; · source
Types
Deque<T, A>
type struct Deque<T, A = GlobalAllocator> where A: Allocator {
private buffer: RawBuffer<T, A>;
private head: i64;
private length: i64;
}
A double-ended queue, growable at both ends. The elements live in one ring buffer: they start at the front slot and run length slots on, wrapping from the buffer's end to its start. Pushing and popping at either end is O(1), amortized over the doublings that grow the buffer, and indexing is O(1). A deque allocates nothing until the first push.
import std::collections::deque::{ Deque };
mut queue: Deque<i64> = Deque::new();
queue.push_back(2);
queue.push_front(1);
queue.push_back(3);
const first: Option<i64> = queue.pop_front(); // Some(1)
const middle: i64 = queue[0]; // 2
front and back borrow the end elements and get_reference(index) any one, each answering None when there is none; queue[i] reads and writes element i from the front. as_slices hands the elements out front to back as the two runs they occupy — the run up to the buffer's end, and the run that wrapped to its start, empty when nothing wrapped. clear releases every element and keeps the buffer.
Constructors
static new() -> Deque<T, GlobalAllocator>;
static new() -> Deque<T, GlobalAllocator>;An empty deque backed by GlobalAllocator. It allocates nothing until the first push.
static new_in(alloc: A) -> Deque<T, A>;
static new_in(alloc: A) -> Deque<T, A>;An empty deque that allocates through alloc.
static with_capacity(capacity: i64) -> Deque<T, GlobalAllocator>;
static with_capacity(capacity: i64) -> Deque<T, GlobalAllocator>;An empty deque with room for capacity elements before it grows. Panics on allocation failure.
Methods
length(&this) -> i64;
length(&this) -> i64;The number of elements in the deque.
is_empty(&this) -> boolean;
is_empty(&this) -> boolean;True when the deque has no elements.
capacity(&this) -> i64;
capacity(&this) -> i64;How many elements fit before the deque grows.
push_back(mut &this, value: T) -> void;
push_back(mut &this, value: T) -> void;Add value after the back element. Panics on allocation failure.
push_front(mut &this, value: T) -> void;
push_front(mut &this, value: T) -> void;Add value before the front element. Panics on allocation failure.
pop_front(mut &this) -> Option<T>;
pop_front(mut &this) -> Option<T>;Remove and return the front element, or None when empty.
pop_back(mut &this) -> Option<T>;
pop_back(mut &this) -> Option<T>;Remove and return the back element, or None when empty.
front(&this) -> Option<T*>;
front(&this) -> Option<T*>;The front element, or None when empty.
back(&this) -> Option<T*>;
back(&this) -> Option<T*>;The back element, or None when empty.
get_reference(&this, index: i64) -> Option<T*>;
get_reference(&this, index: i64) -> Option<T*>;The element index places from the front, or None past the end.
as_slices(&this) -> Pair<&Slice<T>, &Slice<T>>;
as_slices(&this) -> Pair<&Slice<T>, &Slice<T>>;The elements front to back, as the run from the front element to the buffer's end and the run that wrapped round to its start. The second is empty when nothing wrapped.
clear(mut &this) -> void;
clear(mut &this) -> void;Release every element, keeping the buffer.
Trait implementations
implement<T, A> trait Drop for struct Deque<T, A> where A: Allocator
implement<T, A> trait Index<i64> for struct Deque<T, A> where A: Allocator
implement<T, A> trait IndexMutable<i64> for struct Deque<T, A> where A: Allocator
implement<T, A> trait Display for struct Deque<T, A> where T: Display, A: Allocator
std::fmt::display
implement<T, A> trait Debug for struct Deque<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 Deque<T, A = GlobalAllocator> where A: Allocator {}A double-ended queue: elements are pushed and popped at either end in constant time, in a ring buffer that grows when full.
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.
Deque.buffer: RawBuffer<T, A>type struct RawBuffer<T, A = GlobalAllocator> where A: Allocator {}Room for capacity values of T from allocator A, none of them tracked as initialized: the storage a growable container builds on. The container records which slots hold values and drops them; the buffer releases only the storage.
type parameter TDeque.head: i64Deque.length: i64import 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).
type struct Deque<T, A = GlobalAllocator> where A: Allocator {}A double-ended queue: elements are pushed and popped at either end in constant time, in a ring buffer that grows when full.
std::collections::dequemut 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 queue: Deque<i64>static Deque::new() -> Deque<T, GlobalAllocator>An empty deque backed by GlobalAllocator. It allocates nothing until the first push.
mut queue: Deque<i64>Deque::push_back(mut &this, value: T) -> voidAdd value after the back element. Panics on allocation failure.
Deque::push_front(mut &this, value: T) -> voidAdd value before the front element. 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 first: 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.
Deque::pop_front(mut &this) -> Option<T>Remove and return the front element, or None when empty.
const middle: i64Deque::get_reference(&this, index: i64) -> Option<T*>The element index places from the front, or None past the end.
Option::NoneDeque::as_slices(&this) -> Pair<&Slice<T>, &Slice<T>>The elements front to back, as the run from the front element to the buffer's end and the run that wrapped round to its start. The second is empty when nothing wrapped.
std::collections::dequestatic 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 Deque::new_in(alloc: A) -> Deque<T, A>An empty deque that allocates through alloc.
(parameter) alloc: Astatic Deque::with_capacity(capacity: i64) -> Deque<T, GlobalAllocator>An empty deque with room for capacity elements before it grows. Panics on allocation failure.
(parameter) capacity: i64&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.
mut &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: TDeque::pop_back(mut &this) -> Option<T>Remove and return the back element, or None when empty.
(parameter) index: i64type struct Pair<A, B> with [Eq, Clone] {
first: A;
second: B;
}Two values held together, first and second: a map's key with its value.
type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
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 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 Drop {
drop(mut &this) -> void;
}A type that releases something when a value of it ends: memory, a file handle, a lock. The compiler calls drop once when an owned value goes out of scope; release a value early with mem::drop(value).
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 Index<Idx> {
type Output;
index(&this, idx: Idx) -> const This::Output*;
}Index<Idx> is the contract for reading container[idx], returning a read-only pointer to the element, its Output. The compiler rewrites a[i] into *(a.index(i)) where a[i] is read (v = a[i]).
type trait IndexMutable<Idx> : Index<Idx> {
index_mutable(mut &this, idx: Idx) -> mut This::Output*;
}IndexMutable<Idx> is the contract for writing container[idx], returning a writable pointer to the Output of its Index<Idx>.
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.