hashmap
import std::collections::hashmap; · source
Types
HashMap<K, V, A>
type struct HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
static new() -> HashMap<K, V, GlobalAlloc>;
static new_in(alloc: A) -> HashMap<K, V, A>;
static with_seed(seed: u64) -> HashMap<K, V, GlobalAlloc>;
static with_seed_in(seed: u64, alloc: A) -> HashMap<K, V, A>;
static with_capacity(capacity: u64) -> HashMap<K, V, GlobalAlloc>;
length(&this) -> u64;
is_empty(&this) -> boolean;
bucket_count(&this) -> u64;
get_bucket(&this, index: u64) -> Entry<K, V>*;
iter(&this) -> implement Iterator<Pair<K, V>>
where K: Copy, V: Copy;
keys(&this) -> implement Iterator<K>
where K: Copy;
values(&this) -> implement Iterator<V>
where V: Copy;
entries(&this) -> implement Iterator<Pair<K*, V*>>;
keys_ref(&this) -> implement Iterator<K*>;
values_ref(&this) -> implement Iterator<V*>;
contains_key(&this, key: &K) -> boolean
where K: Hash + Eq;
get(&this, key: &K) -> Option<V>
where K: Hash + Eq, V: Copy;
get_ref(&this, key: &K) -> Option<V*>
where K: Hash + Eq;
insert(mut &this, key: K, value: V) -> Option<V>
where K: Hash + Eq;
try_insert(mut &this, key: K, value: V) -> Result<Option<V>, AllocError>
where K: Hash + Eq;
get_or_insert(mut &this, key: K, default: V) -> V*
where K: Hash + Eq;
try_get_or_insert(mut &this, key: K, default: V) -> Result<V*, AllocError>
where K: Hash + Eq;
remove(mut &this, key: &K) -> Option<V>
where K: Hash + Eq;
clear(mut &this) -> void;
resize_buckets(mut &this, new_count: u64) -> Result<(), AllocError>;
}
Separate chaining over a bucket array: each bucket holds a linked list of Entry nodes. Keys must implement Hash + Eq; the bound sits on each method rather than the type, so a map of un-hashable keys is spellable but unusable.
import std::collections::hashmap;
mut counts: HashMap<Str, u64> = HashMap::<Str, u64>::new();
mut slot: u64* = counts.get_or_insert(word, 0);
*slot = *slot + 1;
get reads by value and needs V: Copy; get_ref and get_or_insert hand back a V* and are sound for owning values. insert returns the value it replaced, and remove drops the key and returns the value. bucket_count is buckets allocated, not capacity — chaining means one bucket holds many entries — and get_bucket is the head of a chain for manual traversal. try_insert and try_get_or_insert are the fallible forms.
Properties
| Property | Type | Notes |
|---|---|---|
buckets | Entry<K, V>** | |
bucket_count | u64 | |
length | u64 | |
alloc | A | |
seed | u64 | Per-map hash seed (initial DefaultHasher state). Drawn from the process seed generator (next_map_seed) so bucket placement is unpredictable between runs; fixed via with_seed* for tests that need reproducible iteration/layout. |
Constructors
static new() -> HashMap<K, V, GlobalAlloc>;
static new_in(alloc: A) -> HashMap<K, V, A>;
static with_seed(seed: u64) -> HashMap<K, V, GlobalAlloc>;
static with_seed(seed: u64) -> HashMap<K, V, GlobalAlloc>;Construct an empty map with an explicit, fixed hash seed instead of a random one. Intended for tests and any caller that needs reproducible bucket placement / iteration order; production code should prefer new so the seed stays unpredictable.
static with_seed_in(seed: u64, alloc: A) -> HashMap<K, V, A>;
static with_seed_in(seed: u64, alloc: A) -> HashMap<K, V, A>;with_seed in a caller-supplied allocator.
static with_capacity(capacity: u64) -> HashMap<K, V, GlobalAlloc>;
static with_capacity(capacity: u64) -> HashMap<K, V, GlobalAlloc>;Preallocate for at least capacity entries at the default load factor. The actual bucket count is the smallest power of two that keeps the load factor below 1.0.
Methods
length(&this) -> u64;
is_empty(&this) -> boolean;
bucket_count(&this) -> u64;
bucket_count(&this) -> u64;Number of buckets currently allocated. Not the same as the number of entries the map can hold; chaining means every bucket can hold many entries.
get_bucket(&this, index: u64) -> Entry<K, V>*;
get_bucket(&this, index: u64) -> Entry<K, V>*;Return the head of the entry chain for bucket index. Returns null if the bucket is empty or index is out of range. The returned pointer is valid until the next mutation of the map.
iter(&this) -> implement Iterator<Pair<K, V>> where K: Copy, V: Copy;
iter(&this) -> implement Iterator<Pair<K, V>> where K: Copy, V: Copy;Iterate over every entry, yielding Pair<K, V> (key first, value second). Iteration order is bucket-major and undefined across resizes; do not rely on it.
keys(&this) -> implement Iterator<K> where K: Copy;
keys(&this) -> implement Iterator<K> where K: Copy;Iterate over the keys, yielding each by value (bitwise copy). Gated on K: Copy for the same reason as iter. Order is bucket-major and unstable across resizes.
values(&this) -> implement Iterator<V> where V: Copy;
values(&this) -> implement Iterator<V> where V: Copy;Iterate over the values, yielding each by value (bitwise copy). Gated on V: Copy. Order is bucket-major and unstable.
entries(&this) -> implement Iterator<Pair<K*, V*>>;
entries(&this) -> implement Iterator<Pair<K*, V*>>;Iterate over every entry, yielding Pair<K*, V*> (borrowed key and value). Unlike iter, this is sound for any K/V (owning or Copy) because it hands out borrows, never bitcopies - so it is the way to walk a HashMap<String, Foo> without cloning. The pointers borrow map-owned storage and are invalidated by any mutation that inserts, removes, or resizes; same contract as get_ref. Order is bucket-major and unstable across resizes.
keys_ref(&this) -> implement Iterator<K*>;
keys_ref(&this) -> implement Iterator<K*>;Iterate over the keys by shared borrow, yielding K*. Sound for any K (owning or Copy); this is how to walk the keys of a HashMap<String, V> - the common case - without the K: Copy gate on keys. Same borrow/invalidation contract as entries.
values_ref(&this) -> implement Iterator<V*>;
values_ref(&this) -> implement Iterator<V*>;Iterate over the values by shared borrow, yielding V*. Sound for any V (owning or Copy). Same borrow/invalidation contract as entries; use this to read or mutate owning values in place without the V: Copy gate on values.
contains_key(&this, key: &K) -> boolean where K: Hash + Eq;
get(&this, key: &K) -> Option<V> where K: Hash + Eq, V: Copy;
get(&this, key: &K) -> Option<V> where K: Hash + Eq, V: Copy;Look up a value by key, returning a bitwise copy. Gated on V: Copy for the same reason as Array::get: handing back a duplicate of an owning value would alias storage the map still owns, double-freeing on drop. For owning V, borrow in place with get_ref (or move it out with remove).
get_ref(&this, key: &K) -> Option<V*> where K: Hash + Eq;
get_ref(&this, key: &K) -> Option<V*> where K: Hash + Eq;Like get, but returns a borrow (raw pointer) into the entry's stored value rather than a copy. Use when the value owns heap resources you don't want to clone, or when callers need to mutate the stored value in place.
insert(mut &this, key: K, value: V) -> Option<V> where K: Hash + Eq;
insert(mut &this, key: K, value: V) -> Option<V> where K: Hash + Eq;Insert key -> value. Returns the previous value if the key was present, otherwise None. Panics on allocation failure; use try_insert to recover from OOM.
try_insert(mut &this, key: K, value: V) -> Result<Option<V>, AllocError> where K: Hash + Eq;
get_or_insert(mut &this, key: K, default: V) -> V* where K: Hash + Eq;
get_or_insert(mut &this, key: K, default: V) -> V* where K: Hash + Eq;Borrow the value for key, inserting default first if the key is absent. Returns a pointer to the (existing or freshly inserted) value, valid until the next mutation that removes this key or drops the map (a resize keeps entry storage in place). A single lookup, unlike get_ref-then-insert. Panics on allocation failure; the common case (key present) never allocates.
try_get_or_insert(mut &this, key: K, default: V) -> Result<V*, AllocError> where K: Hash + Eq;
try_get_or_insert(mut &this, key: K, default: V) -> Result<V*, AllocError> where K: Hash + Eq;Fallible get_or_insert: returns Err(AllocError) instead of panicking when a needed resize or entry allocation fails.
remove(mut &this, key: &K) -> Option<V> where K: Hash + Eq;
remove(mut &this, key: &K) -> Option<V> where K: Hash + Eq;Remove the entry with matching key. Returns the removed value, or None if the key wasn't present.
clear(mut &this) -> void;
clear(mut &this) -> void;Drop every entry. Retains the bucket array so subsequent inserts don't re-pay the allocation.
resize_buckets(mut &this, new_count: u64) -> Result<(), AllocError>;
Entry<K, V>
type struct Entry<K, V> {
hash: u64;
key: K;
value: V;
next: Entry<K, V>*;
}
One node of a bucket's chain. get_bucket hands back the head of a chain as an Entry*, and next walks it; the cached hash is what lets a resize redistribute entries without re-hashing the keys. You only touch it for manual traversal.
Properties
| Property | Type |
|---|---|
hash | u64 |
key | K |
value | V |
next | Entry<K, V>* |
Iterating
iter(), keys(), and values() yield by value and carry Copy bounds. entries(), keys_ref(), and values_ref() yield Pair<K*, V*>, K*, and V* respectively, and are sound for any key and value type — so they are what you want for a HashMap<String, Foo>. Order is bucket-major and unstable across resizes; every borrow is invalidated by any insert, remove, or resize.
Hash seeding
The hasher is FNV-1a, seeded per map from a per-process random value drawn from the kernel CSPRNG. Bucket placement therefore varies between runs, so adversarial keys cannot be precomputed to flood a single bucket. FNV-1a is not collision-resistant, so treat this as hardening rather than a cryptographic guarantee — supply a custom hasher where it matters.
with_seed(seed) and with_seed_in(seed, alloc) fix the seed for tests that need reproducible layout or iteration order. Production code should use new.
Chaining was chosen over open addressing because it sidesteps storing uninitialized key/value slots. The cost is one heap allocation per entry.
Trait implementations
implement<K, V, A> trait Drop for struct HashMap<K, V, A> where A: Allocator
implement<K, V> trait Clone for struct HashMap<K, V, GlobalAlloc> where K: Hash + Eq + Clone, V: Clone
implement<K, V> trait Default for struct HashMap<K, V, GlobalAlloc>
implement<K, V, A> trait Display for struct HashMap<K, V, A> where K: Display, V: Display, A: Allocator
std::fmt::display
implement<K, V, A> trait Debug for struct HashMap<K, V, A> where K: Debug, V: Debug, A: Allocator
std::fmt::display
Clone re-inserts every entry into a fresh map, so the clone's bucket layout may differ. Default is a fresh empty map that allocates no buckets until the first insert. Display renders {k: v, ...}.
Constants
const INITIAL_BUCKETS: u64 = 8;
const SEED_GAMMA: u64 = 0x9E3779B97F4A7C15;
const SEED_MIX_1: u64 = 0xBF58476D1CE4E5B9;
const SEED_MIX_2: u64 = 0x94D049BB133111EB;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 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.
type parameter Ktype parameter Vtype parameter Awhere A: Allocator
type struct GlobalAlloc {}Zero-sized handle to the process-wide allocator. Construct with GlobalAlloc::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>, AllocError>;
deallocate(mut &this, ptr: NonNull<u8>, layout: Layout) -> void;
reallocate(mut &this, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<u8>, AllocError>;
}Allocator: the trait every heap strategy implements.
std::alloc::allocatorHashMap.buckets: Entry<K, V>**type struct Entry<K, V> {
hash: u64;
key: K;
value: V;
next: Entry<K, V>*;
}type parameter Ktype parameter VHashMap.bucket_count: u64HashMap.length: u64HashMap.alloc: AHashMap.seed: u64Per-map hash seed (initial DefaultHasher state). Drawn from the process seed generator (next_map_seed) so bucket placement is unpredictable between runs; fixed via with_seed* for tests that need reproducible iteration/layout.
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 HashMap::new() -> HashMap<K, V, GlobalAlloc>type 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::hashmapstatic HashMap::new_in(alloc: A) -> HashMap<K, V, A>(parameter) alloc: Astatic HashMap::with_seed(seed: u64) -> HashMap<K, V, GlobalAlloc>Construct an empty map with an explicit, fixed hash seed instead of a random one. Intended for tests and any caller that needs reproducible bucket placement / iteration order; production code should prefer new so the seed stays unpredictable.
(parameter) seed: u64static HashMap::with_seed_in(seed: u64, alloc: A) -> HashMap<K, V, A>with_seed in a caller-supplied allocator.
static HashMap::with_capacity(capacity: u64) -> HashMap<K, V, GlobalAlloc>Preallocate for at least capacity entries at the default load factor. The actual bucket count is the smallest power of two that keeps the load factor below 1.0.
(parameter) capacity: u64&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.
HashMap::is_empty(&this) -> booleanHashMap::get_bucket(&this, index: u64) -> Entry<K, V>*Return the head of the entry chain for bucket index. Returns null if the bucket is empty or index is out of range. The returned pointer is valid until the next mutation of the map.
(parameter) index: u64HashMap::iter(&this) -> implement Iterator<Pair<K, V>>
where K: Copy, V: CopyIterate over every entry, yielding Pair<K, V> (key first, value second). Iteration order is bucket-major and undefined across resizes; do not rely on it.
-> 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) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}type struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairtype 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::markerHashMap::keys(&this) -> implement Iterator<K>
where K: CopyIterate over the keys, yielding each by value (bitwise copy). Gated on K: Copy for the same reason as iter. Order is bucket-major and unstable across resizes.
HashMap::values(&this) -> implement Iterator<V>
where V: CopyIterate over the values, yielding each by value (bitwise copy). Gated on V: Copy. Order is bucket-major and unstable.
HashMap::entries(&this) -> implement Iterator<Pair<K*, V*>>Iterate over every entry, yielding Pair<K*, V*> (borrowed key and value). Unlike iter, this is sound for **any** K/V (owning or Copy) because it hands out borrows, never bitcopies - so it is the way to walk a HashMap<String, Foo> without cloning.
HashMap::keys_ref(&this) -> implement Iterator<K*>Iterate over the keys by shared borrow, yielding K*. Sound for any K (owning or Copy); this is how to walk the keys of a HashMap<String, V> - the common case - without the K: Copy gate on keys. Same borrow/invalidation contract as entries.
HashMap::values_ref(&this) -> implement Iterator<V*>Iterate over the values by shared borrow, yielding V*. Sound for any V (owning or Copy). Same borrow/invalidation contract as entries; use this to read or mutate owning values in place without the V: Copy gate on values.
HashMap::contains_key(&this, key: &K) -> boolean
where K: Hash + Eq(parameter) key: &Ktype trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
HashMap::get(&this, key: &K) -> Option<V>
where K: Hash + Eq, V: CopyLook up a value by key, returning a bitwise copy. Gated on V: Copy for the same reason as Array::get: handing back a duplicate of an owning value would alias storage the map still owns, double-freeing on drop. For owning V, borrow in place with get_ref (or move it out with remove).
HashMap::get_ref(&this, key: &K) -> Option<V*>
where K: Hash + EqLike get, but returns a borrow (raw pointer) into the entry's stored value rather than a copy. Use when the value owns heap resources you don't want to clone, or when callers need to mutate the stored value in place.
HashMap::insert(mut &this, key: K, value: V) -> Option<V>
where K: Hash + EqInsert key -> value. Returns the previous value if the key was present, otherwise None. Panics on allocation failure; use try_insert to recover from OOM.
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) key: K(parameter) value: VHashMap::try_insert(mut &this, key: K, value: V) -> Result<Option<V>, AllocError>
where K: Hash + Eqtype struct AllocError {
kind: AllocErrorKind;
layout: Layout;
}HashMap::get_or_insert(mut &this, key: K, default: V) -> V*
where K: Hash + EqBorrow the value for key, inserting default first if the key is absent. Returns a pointer to the (existing or freshly inserted) value, valid until the next mutation that removes this key or drops the map (a resize keeps entry storage in place). A single lookup, unlike get_ref-then-insert.
(parameter) default: VHashMap::try_get_or_insert(mut &this, key: K, default: V) -> Result<V*, AllocError>
where K: Hash + EqFallible get_or_insert: returns Err(AllocError) instead of panicking when a needed resize or entry allocation fails.
HashMap::remove(mut &this, key: &K) -> Option<V>
where K: Hash + EqRemove the entry with matching key. Returns the removed value, or None if the key wasn't present.
HashMap::clear(mut &this) -> voidDrop every entry. Retains the bucket array so subsequent inserts don't re-pay the allocation.
HashMap::resize_buckets(mut &this, new_count: u64) -> Result<(), AllocError>(parameter) new_count: u64primitive ()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::hashmap;HashMap<K, V, A>: key-value map with amortized O(1) operations.
module pagemut 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 counts: HashMap<Str, u64>type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strstatic HashMap::new() -> HashMap<K, V, GlobalAlloc>mut slot: u64*mut counts: HashMap<Str, u64>HashMap::get_or_insert(mut &this, key: K, default: V) -> V*
where K: Hash + EqBorrow the value for key, inserting default first if the key is absent. Returns a pointer to the (existing or freshly inserted) value, valid until the next mutation that removes this key or drops the map (a resize keeps entry storage in place). A single lookup, unlike get_ref-then-insert.
mut slot: u64*type struct Entry<K, V> {
hash: u64;
key: K;
value: V;
next: Entry<K, V>*;
}Entry.hash: u64Entry.key: Ktype parameter KEntry.value: Vtype parameter VEntry.next: Entry<K, V>*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 INITIAL_BUCKETS: u64 = 8;const SEED_GAMMA: u64 = 0x9E3779B97F4A7C15;const SEED_MIX_1: u64 = 0xBF58476D1CE4E5B9;const SEED_MIX_2: u64 = 0x94D049BB133111EB;HashMap::get(&this, key: &K) -> Option<V>
where K: Hash + Eq, V: CopyLook up a value by key, returning a bitwise copy. Gated on V: Copy for the same reason as Array::get: handing back a duplicate of an owning value would alias storage the map still owns, double-freeing on drop. For owning V, borrow in place with get_ref (or move it out with remove).
HashMap::get_ref(&this, key: &K) -> Option<V*>
where K: Hash + EqLike get, but returns a borrow (raw pointer) into the entry's stored value rather than a copy. Use when the value owns heap resources you don't want to clone, or when callers need to mutate the stored value in place.
HashMap::insert(mut &this, key: K, value: V) -> Option<V>
where K: Hash + EqInsert key -> value. Returns the previous value if the key was present, otherwise None. Panics on allocation failure; use try_insert to recover from OOM.
HashMap::remove(mut &this, key: &K) -> Option<V>
where K: Hash + EqRemove the entry with matching key. Returns the removed value, or None if the key wasn't present.
HashMap.bucket_count: u64HashMap::get_bucket(&this, index: u64) -> Entry<K, V>*Return the head of the entry chain for bucket index. Returns null if the bucket is empty or index is out of range. The returned pointer is valid until the next mutation of the map.
HashMap::try_insert(mut &this, key: K, value: V) -> Result<Option<V>, AllocError>
where K: Hash + EqHashMap::try_get_or_insert(mut &this, key: K, default: V) -> Result<V*, AllocError>
where K: Hash + EqFallible get_or_insert: returns Err(AllocError) instead of panicking when a needed resize or entry allocation fails.
HashMap.buckets: Entry<K, V>**HashMap.length: u64HashMap.alloc: AHashMap.seed: u64Per-map hash seed (initial DefaultHasher state). Drawn from the process seed generator (next_map_seed) so bucket placement is unpredictable between runs; fixed via with_seed* for tests that need reproducible iteration/layout.
type struct DefaultHasher {
state: u64;
}64-bit FNV-1a. Simple; not DoS-resistant. Fine as a HashMap default when inputs are trusted.
std::core::hashfunction next_map_seed() -> u64Return the next per-map seed, initializing the process seed from the kernel CSPRNG on first use.
std::collections::hashmapstatic HashMap::with_seed(seed: u64) -> HashMap<K, V, GlobalAlloc>Construct an empty map with an explicit, fixed hash seed instead of a random one. Intended for tests and any caller that needs reproducible bucket placement / iteration order; production code should prefer new so the seed stays unpredictable.
static HashMap::new_in(alloc: A) -> HashMap<K, V, A>new T { field: value }
new E::Variant(args)
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V(args) does the same for an enum variant; new T[n] reserves n uninitialised T - the typed malloc. The storage comes from T's Allocate implementation, or else the global allocator. Parentheses after a struct's name are refused (E0117): there are no constructors, and a struct's static new(..) is an ordinary method.
static HashMap::with_seed_in(seed: u64, alloc: A) -> HashMap<K, V, A>with_seed in a caller-supplied allocator.
static HashMap::with_capacity(capacity: u64) -> HashMap<K, V, GlobalAlloc>Preallocate for at least capacity entries at the default load factor. The actual bucket count is the smallest power of two that keeps the load factor below 1.0.
HashMap::is_empty(&this) -> booleanHashMap::iter(&this) -> implement Iterator<Pair<K, V>>
where K: Copy, V: CopyIterate over every entry, yielding Pair<K, V> (key first, value second). Iteration order is bucket-major and undefined across resizes; do not rely on it.
HashMap::keys(&this) -> implement Iterator<K>
where K: CopyIterate over the keys, yielding each by value (bitwise copy). Gated on K: Copy for the same reason as iter. Order is bucket-major and unstable across resizes.
HashMap::values(&this) -> implement Iterator<V>
where V: CopyIterate over the values, yielding each by value (bitwise copy). Gated on V: Copy. Order is bucket-major and unstable.
HashMap::entries(&this) -> implement Iterator<Pair<K*, V*>>Iterate over every entry, yielding Pair<K*, V*> (borrowed key and value). Unlike iter, this is sound for **any** K/V (owning or Copy) because it hands out borrows, never bitcopies - so it is the way to walk a HashMap<String, Foo> without cloning.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringHashMap::keys_ref(&this) -> implement Iterator<K*>Iterate over the keys by shared borrow, yielding K*. Sound for any K (owning or Copy); this is how to walk the keys of a HashMap<String, V> - the common case - without the K: Copy gate on keys. Same borrow/invalidation contract as entries.
HashMap::values_ref(&this) -> implement Iterator<V*>Iterate over the values by shared borrow, yielding V*. Sound for any V (owning or Copy). Same borrow/invalidation contract as entries; use this to read or mutate owning values in place without the V: Copy gate on values.
HashMap::contains_key(&this, key: &K) -> boolean
where K: Hash + Eqtype 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::arrayArray::get(&this, index: u64) -> Option<T>
where T: CopyBounds-checked read by value. Gated on T: Copy: the result is an independent bitwise duplicate while the array retains ownership of the stored element, so handing back a copy is only sound when T carries no Drop obligation.
Entry.key: KOption::None// reservedOpens no form of the language: a declaration or member written with it is refused where it is written (E0117). It still names a method: Default::default() is the trait's constructor.
Result::Err(E)HashMap::clear(mut &this) -> voidDrop every entry. Retains the bucket array so subsequent inserts don't re-pay the allocation.
std::collections::hashmapHashMap::resize_buckets(mut &this, new_count: u64) -> Result<(), AllocError>Entry.next: Entry<K, V>*Entry.hash: u64Entry.value: Vimplement 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.
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 parameter Kwhere K: Hash + Eq + Clone
type parameter Vwhere V: Clone
type trait Default {
static default() -> This;
}Default: a canonical "zero value" for a type.
std::core::defaulttype parameter Kwhere K: Display
type parameter Vwhere V: Display
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype parameter Kwhere K: Debug
type parameter Vwhere V: Debug
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}function name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VaArgs to read typed values from it.