collections
Six containers, all built on alloc and all generic over the allocator with a GlobalAlloc default. Each owns heap storage and exposes drop(mut &this); the compiler synthesises the call at scope exit.
public module collections::raw_buffer;
public module collections::array;
public module collections::str;
public module collections::string;
public module collections::hashmap;
public module collections::hashset;
public module collections::pair;
| File | What it holds | Import |
|---|---|---|
array | Array<T, A>, the growable contiguous sequence | (prelude) |
str | Str, the borrowed UTF-8 view | import std::collections::str; |
string | String<A>, owned growable UTF-8 | import std::collections::string; |
hashmap | HashMap<K, V, A>, separate chaining | import std::collections::hashmap; |
hashset | HashSet<T, A>, over HashMap<T, ()> | import std::collections::hashset; |
pair | Pair<A, B>, the owned two-element tuple | import std::collections::pair; |
raw_buffer | RawBuffer<T, A>, the untyped backing store String is built on | import std::collections::raw_buffer; |
A note on Copy-gated methods
Several methods across these containers carry a where T: Copy bound — get, iter, append, resize. The reason is the same every time: they hand back an independent bitwise duplicate while the container still owns the stored element. That is only sound when T has no drop obligation; otherwise both copies would own the same heap buffer and both would free it.
For an owning element type, reach for the borrowing counterpart instead: get_ref rather than get, iter_ref rather than iter, entries / keys_ref / values_ref on a map. Those hand out pointers, never copies, and are sound for every T.
A second family of bounds, where T: Drop, appears on the methods that discard elements — set, truncate, clear, remove on a map. Every type satisfies it (a POD type's drop lowers to nothing), so it restricts nothing; it is what lets the container run T::drop on the elements it throws away instead of leaking them.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
namespace std::collections::raw_buffer;RawBuffer: the allocator-backed storage half of a growable container.
module pagenamespace std::collections::array;Array<T, A>: growable, heap-backed, contiguous sequence.
module pagenamespace std::collections::hashmap;HashMap<K, V, A>: key-value map with amortized O(1) operations.
module pagenamespace std::collections::hashset;HashSet<T, A>: unordered collection of unique values.
module pagetype struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::new(); all instances dispatch through the same underlying backend.
function drop<T>(_v: T) -> voidExplicitly drop a value of type T. This is a no-op for types that do not implement a destructor. The caller is responsible for ensuring that any resources held by _v are properly released if necessary.
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.
&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.
type struct Array<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
length: i64;
capacity: i64;
alloc: A;
}Array<T, A>: growable, heap-backed, contiguous sequence.
std::collections::arraytype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strimport 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 String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringtype struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmaptype struct HashSet<T, A = GlobalAlloc> {
map: HashMap<T, (), A>;
}HashSet<T, A>: unordered collection of unique values.
std::collections::hashsetprimitive ()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.
type struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairtype struct RawBuffer<T, A = GlobalAlloc> where A: Allocator {
ptr: T*;
capacity: u64;
alloc: A;
}RawBuffer: the allocator-backed storage half of a growable container.
std::collections::raw_bufferwhere 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 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::marker