ADR-collections-v4: A Map Read Is a T? and a Key Is Not a Position; the Ordered Collections Offer What the Hashed Ones Offer, and an Array and a Set Are Walked as a List Is; a Sequence Says What It Is as a Whole; and What Is Handed Over Is Gone, Part by Part

Status: Accepted Date: November 30, 2026 (the consolidation, #2925); D1-D9 October 29 to November 15, 2026; D10, D11 October 31, 2026 (architect, #1082); D12-D43 October 3, 2026 History: the records this one replaces are archived in docs/history/adr; read them only for how the decisions were reached Answers: whether BTreeMap and BTreeSet get entry and extend, which HashMap and Vec have and which were NK1171 on them (#950); whether every map has extend (#1605); whether Array[T, N] gets iter() and the sequence surface of Vec::iter(), which was NK1171 (#955); whether BTreeSet and HashSet get it too (#1006); whether std has Entry::or_default and Seq::cloned, which the C1a move (contracts/mod.rs) needed (#1082); #261 (a map read through the brackets aborts on an absent key); #208 (collect() into a declared map passes the checker and fails in rustc); open-decisions.md’s what a read of a map of T? values is, met moving the sharing analysis into Nikaia (#125); and the owner’s decisions on outside feedback: the repairs first (a range in a name, a named keys(), a sequence taken by a let, a loop or a lambda all reached rustc), reversing a pipeline must not force a collect(), repair the owned-key map, and fix the open points those repairs left Supersedes / Related: takes every decision on collections from the three records now in the history: the entry, extend and iter() decisions of the record before keep their numbers D1-D9; Entry::or_default and Seq::cloned are D10 and D11; the map read, the sequence and the taking decisions are D12-D43; the decision on a sequence’s pipeline (one loop, none of it allocating) is ADR-chain-loop-v2 D8, with its lowering D6 there; ADR-288 D11 (what std offers is decided by the coherence of its types; stands, applied here); ADR-253 D2 (a.extend(b) joins a list to another): changed by D3 to the argument every extend takes; ADR-282 D7, D9 (a copy is the one the program writes; the compiler writes the one below); ADR-080 D2 (a write through the brackets is an insert); ADR-278 (T? is a type, null a word, ?. reaches through one); ADR-105 D1, D2, D4 (Seq[T], its words, the once-only rule NK2702, no Seq in a program’s type grammar); ADR-291 D13 (a range is an ordinary expression and may be named); ADR-094 D2, D4 (keeps; a for lends); ADR-tether-v1 D3 (the compiler inserts no copy); ADR-grammar-lowering-v1 D2 (the emitter does not know types); ADR-278 D20 (?? joins two views; an owned fallback beside a view is NK1185); ADR-279 D5 (what ?? does to its left side); ADR-282 D8 (.clone() is the copy, .to_owned() is NK1189); ADR-282 D4, D5, D25 (where a literal is built into text; a view kept needs a copy); ADR-282 D12, D16 (text positions and either_items); ADR-184 D3 (ref Array[T], a run somebody else keeps); ADR-182 D2 (a slice read carries no *); ADR-233 D1, D4 (a sequence whose step pauses; a view of a value that copies is read as the value); ADR-285 D1 (no usize a program meets); ADR-300 D10 (an abort reports the Nikaia line); ADR-280 D5, D9 (what std throws, and a named channel); Part I 3.3 (a for lends its list), 4.5 (Array[T, N], maps, lists), 6.5 (what is handed over), Part III 13.5 (Seq’s words, collect), 17.1 (std::collections, Seq[T]), 17 (every Rust std call a move moves has a counterpart)

1. Question

What does a container offer, what does it hand out, and what happens to what is handed in? Four questions that each answer part of the others:

Without one answer per question a first program meets a wall - an absent key that ends the program, a named range or keys() that rustc will not walk, an owned key that does not compile, a collect() into a map that writes a Vec, a second use of a pushed value, an ordered map with less than a hashed one - and the checker passes what the language below refuses (Part III C.1).

2. How others do it

Language / tool Its answer Source
Rust entry on HashMap and BTreeMap, each with its own Entry, and Entry::or_default; extend on every collection (Extend<T>, maps from pairs); [T; N], [T] and every set have iter(), and Iterator::cloned with copied beside it. m[k] aborts on an absent key; m[k] = v is refused altogether, so the asymmetry never arises. DoubleEndedIterator (from the back) and ExactSizeIterator (length known) are two traits, and take, zip, skip, step_by need both to reverse. Range<i64> has no ExactSizeIterator. Option<&Option<V>> from a map of options standard library signatures, std docs, from memory, not checked
Java computeIfAbsent, merge, putAll on the Map interface, so TreeMap and HashMap alike; addAll on every Collection; TreeSet.iterator() in order. ConcurrentHashMap refuses null values, for a two-step race from memory, not checked
C++ try_emplace, insert(first, last) on std::map and std::unordered_map alike; std::array and std::set have begin/end as std::vector does from memory, not checked
Python d[k] raises on an absent key; d.get(k) answers None; setdefault, update on dict; set.update from memory, not checked
Kotlin, TypeScript, Dart a map of V? reads one V?; containsKey / has tells absent from stored null from memory, not checked
Scala, Haskell nested: Option[Option[V]], Maybe (Maybe v), flattened by the program from memory
Swift a fixed array is a Sequence like any other; [K: V?] reads a V??, and dict[k] = nil removes the key from memory, not checked
Go, C# range walks an array as it walks a slice; one step answers both absent and present: v, ok := m[k], TryGetValue language specification

No language in the field gives its ordered map fewer of these entries than its hashed one: the two differ in order and in the bound on the key, not in what a program does with them. An array and an ordered set are walked through the iterator the list has, and no pipeline is put on the set itself.

What does not transfer. Rust refuses the bracket write; this language has it (ADR-080 D2), so keeping Rust’s aborting read would leave the most common absence the one spelling that crashes. A nullable here never nests (Part I 2.3), which puts the language in Kotlin’s family for a map of T?. Java’s refusal of null values is a concurrency reason that a door over a shared map removes. And Range<i64>’s missing length is a fact about Rust’s usize on a 32-bit target, which a range in this language must not inherit. Rust’s copied beside cloned follows its split of Copy from Clone, which a program here does not write.

3. Options, weighed

For the surface of each kind (D1-D9):

  D1 the special case pays D2 the compiler works D3 no cost for the common case D4 consistent If it is wrong
A nobody one contract block, the lowering is Rust’s own method one lookup per update, one call per join, no copy per walk a reader who learns entry on one map has it on the other; an array reads as a list of fixed length a later door of the language’s own replaces entry on both at once
B the ordered map’s user - a second lookup per update a map kind with less, for no reason a reader can see -
C every program with an ordered map, every walk over an array the developer writes the loop or the copy the loop, the second lookup, an allocation per walk two map kinds that differ in surface; an array indexed as a list but not walked as one the loops stay
D - the ledger doubles per set kind - each thing said two ways; open whether s.filter gives a set or a sequence; nothing in Rust to lower to -

Entry::or_default and Seq::cloned (D10, D11) were weighed by the principles alone: whoever uses it pays (two entries in the ledger; a program that does not call them sees nothing); consistency (or_default is or_insert_with with the constructor the language already writes as V(); cloned is the .clone() each element already takes, said once for the whole sequence, as extend says it); other languages (Rust has both).

For the map read, the sequence and the taking (D12-D43):

Question Rejected Why it lost
a map read keep the abort absence is ordinary for a key that comes from data; the crash is the one place the program does not have to answer a T?
?? over a view and a value copy the view out with Clone an allocation nobody wrote (ADR-tether-v1 D3)
a map of T? refuse T? as a value type a second container for every program with an absent value, for Java’s concurrency reason, which doors remove
a map of T? keep the two answers apart (T??) every read pays the rare question, and it is the nesting Part I 2.3 rules out
what a sequence can do backwards one word, reversible wrong either way: refuses chars().rev() (no length) or allows lines.filter(f).zip(ys).rev() (no length where one is needed)
reversing a pipeline rev only on containers every reversal of a pipeline a collect() first - the heap copy the language exists to avoid
a stream narrowed from both ends a cursor with next() / next_back() a new abstraction rather than a word on Seq; index arithmetic on views and a grammar already cover it. Left out by the owner until a program needs it
collect() into a declared map refuse it (Seq::collect hands back Vec[$T]) a wall; the owner chose build what the place declares
a key in the brackets wrap it in index::at, as a position index::at makes a number a usize and passes a view through: an owned key did not compile, an i64 key became a usize
a taking within one statement order reads by statement only keep(name, name) and s.count() + s.count() reached rustc

4. Decision

4.1 — What each collection offers

D1 — BTreeMap::entry is HashMap::entry

m.entry(k) on a BTreeMap names the place of k as it does on a HashMap, and or_insert(v), or_insert_with(f) and and_modify(f) behave the same: the map keeps the key, or_insert keeps its value, the lambdas run during the call. Entry[V] is one type in the language for both; below it is Rust’s btree_map::Entry or hash_map::Entry, the lowering’s choice.

D2 — Every set has extend

s.extend(items) on a HashSet or a BTreeSet puts every element of items into the set, as insert would one by one. The set keeps them, so items is given away, as Vec::extend’s is.

D3 — extend takes the same argument wherever it stands

On a Vec, a HashSet and a BTreeSet, extend takes a list, a set or a sequence of owned T (s.extend(xs), s.extend(other), s.extend(xs.drain())). It is lowered to Rust’s extend.

D4 — Joining a list that is only read copies its elements, not the list

s.extend(xs.clone()), where xs is used again, is the program’s copy of each element (ADR-282 D7); the compiler writes no list in between and lowers it to s.extend(xs.iter().cloned()) (ADR-282 D9, ADR-258’s third weight). So the one call costs what the loop for x in xs { s.insert(x.clone()) } costs.

D5 — The rule from here on

An entry of HashMap or HashSet that does not depend on hashing is also an entry of BTreeMap or BTreeSet, with the same contract, and the other way round for every entry that does not depend on order (first, last, ranges and union’s order stay the ordered kind’s). A missing one is a defect (CLAUDE.md, kind 3), not a question for a decision round.

D6 — Every map has extend

m.extend(pairs) on a HashMap or a BTreeMap puts every pair into the map, as insert would one by one: a later pair with a key already in the map replaces its value, as a later insert does. Following D3, the argument is another map of the same key and value types, a list of (K, V) or a sequence of owned pairs (m.extend(other), m.extend(pairs), m.extend(other.drain())); it is given away, and D4’s copy of each element applies where it is read again. Lowered to Rust’s extend. It is the form D2 gives every set and ADR-253 D2 every list, and D5 says what one map kind has, the other has too; Rust’s Extend<(K, V)>, Python’s dict.update and Java’s putAll do the same.

D7 — a.iter() on an array is Vec::iter()

On Array[T, N] and on a run ref Array[T] (Part I 3.3), a.iter() hands back the elements one at a time, lent, as the sequence Vec::iter() hands back (Seq[ref(self) T] sync ends sized): it can be walked from either end, its length is known, and every method of a sequence follows (zip, map, filter, enumerate, rev, take, skip, fold, collect). It is lowered to Rust’s .iter() on [T; N] and on [T]. Refusing it on purpose, with .to_vec() first, would be a copy the program does not need (ADR-282 D7), on the type whose point is that nothing is allocated.

D8 — A for lends an array as it lends a list

for x in a walks the array without taking it, and x is a view of its element, under the rules of Part I 3.3.

D9 — Every set has iter(), in its order

s.iter() on a BTreeSet[T] hands back the elements one at a time, lent, smallest first, as Vec::iter() hands back a list’s (Seq[ref(self) T] sync ends sized); on a HashSet[T] it hands back the same sequence in the set’s order, which is none, as HashMap::keys’s (Seq[ref(self) T] sync sized: it does not run backwards). Every method of a sequence follows (any, all, filter, map, count, fold, collect). It is lowered to Rust’s .iter().

Why: a set is already walked by for (Part I 3.3), and Vec, arrays and both maps start a pipeline with iter(), keys() or values(); a set without it answers the same walk two ways (ADR-288 D11). Rejected: no iter, with a for loop and a flag for each any, or a copy into a Vec first (a copy the program does not need, ADR-282 D7); and the sequence methods on the set itself (option D), which says each thing two ways, doubles the ledger per set kind, leaves open whether s.filter gives a set or a sequence, and has no counterpart in Rust to lower to.

D10 — Entry::or_default() puts in the value made with no arguments

m.entry(k).or_default() is m.entry(k).or_insert_with(fn { V() }): the value that is there, putting in first the one that V() makes where there is none. It is on both maps, as or_insert_with is (D1). It is offered where V can be made with no arguments: a collection, String (empty), a number (0), bool (false), and a struct every field of which has a default. For another V the call is refused, with the code that refuses a call whose type does not fit (no new code); its help names or_insert_with. It runs nothing of the program’s, so it never pauses (sync = true).

D11 — Seq::cloned() hands on a copy of each element

xs.iter().cloned() is xs.iter().map fn(x) { x.clone() }: a sequence of owned T, each element the program’s copy (ADR-282 D7), as s.extend(xs.clone()) copies each element (D4). On a sequence whose elements are views it is a copy of each item, in the body of the chain’s loop (ADR-chain-loop-v2 D6) and Rust’s cloned() where no loop of a chain holds it; on one whose elements are already owned, the map it means, or nothing where the compiler knows nothing else holds them. It passes sync, pauses, throws, ends and sized through as map does. One word is enough: Nikaia does not split Copy from Clone in what a program writes, so there is no copied.

4.2 — A map read through the brackets

D12 — On a map, m[k] reads a T?.

let n = counts[fragment]?.n ?? 0
let user = users[request.query("name")] ?? return respond_404()
let counts = report.paths[path] ?? panic(f"{path} was a key a moment ago")

The bracket says what get says. ?. reaches through the result and ?? ends the chain. A program that knows the key is present says so on the right of the ??, so an abort is one the program wrote.

D13 — Writing is unchanged, and a compound assignment is written out.

m[k] = v inserts or replaces (ADR-080 D2). m[k] += 1 reads a T?, so the form is m[k] = (m[k] ?? 0) + 1 - the fallback says what an absent key counts as. A compound assignment to a map slot is refused with that line in the message (NK1162).

D14 — A list keeps its abort.

xs[i] is a T, and an i outside the list ends the program (Part III A.2). An index is arithmetic the program controls; a key is data it does not.

D15 — The read is a view, and the emitter writes one call.

m[k] lowers to nikaia_std::index::get, one trait with an output type per container - the element’s place for a sequence, an Option<&V> for a map - so the emitter, which does not know types, writes the same call for both, and the checker, which knows the container, types the expression. A position goes in through index::at; a key into a map with owned keys goes in as D37 says. The value reached is a copy where V copies and a view of the map otherwise.

D16 — ?? joins a view with a value, and the language below decides which.

A map read is an Option<&V>; the fallback is written as the value it stands for. nikaia_std::index::or joins them by two impls that do not overlap - one over Option<T>, one over Option<&T> where T copies - so a number is copied out, a struct stays a view, and the emitter chooses nothing. Where the value does not copy the fallback is a view too; an owned fallback beside a view is NK1185 (ADR-278 D20), and a copy is written .clone() (ADR-282 D8).

D17 — panic(message) exists, because it is D12’s own way out.

It hands back never, so it stands where a value is wanted, and it reports the Nikaia line through the hook ADR-300 D10 installs. It is on Part I 1.3’s list and Part III A.2’s, and it is not assert.

D18 — A view of a T? is a nullable view.

&str? is Part I 2.3’s own spelling: the checker types let s = &m[k] as a nullable view, so s.min meets NK1125 and not rustc.

D19 — A write through the brackets binds its value first.

m[k] = (m[k] ?? 0) + 1 would otherwise put a &m inside the arguments of a set(&mut m, …). A let below ends the read before the write begins.

D20 — A read is a * over the call, and a map’s answer derefs to the option.

One trait answers a place for a sequence and an option for a map only if the caller writes the same thing for each: a *. For a sequence it is the element in its place - a sequence read cannot be a value below, since moving it out would move out of the container; for a map it takes apart a small wrapper (index::Found) whose Deref hands back the option. The * is written bare; parentheses are added only under a postfix (D43).

D21 — a ?? b on a T? is a T, and the checker says so.

let s = m[k] ?? panic(…) gives s the map’s value type, so s.mean() has a receiver. A ?? over something the checker could not type still claims nothing (Part III C.4).

D22 — io::Lines’ step throws io::IoError.

Beside ADR-280 D5’s list: a step of io::lines yields io::IoError, so a loop in a function with a named channel (ADR-280 D9) has a named error on both sides of its ?.

D23 — A read of a map whose values are T? is one T?.

m[k] and m.get(k) on a map of T? are a T?. An absent key and a key holding null both answer null; m.contains_key(k) tells them apart.

D24 — m[k] = null stores null; removing a key is remove.

After m[k] = null the key is there, contains_key says so and len counts it. The write and the read mean the same null - the trap Swift’s = nil falls into.

D25 — The flattened read is still a view, an Option<&T> below.

The lowering is index::flat, and_then(Option::as_ref) over the read. Nothing is copied, and the read is handed to a ref T? parameter as it is.

D26 — Kept where a T? of its own is wanted, a map’s read is copied out if the value copies, and refused otherwise.

For every map. An annotated let, an assignment, a return, a field or an argument that keeps a T? of its own copies the read out where T copies (ADR-233 D4); where it does not, NK1102 names m[k]?.clone().

4.3 — A sequence

What a sequence’s pipeline allocates between its steps, the type its map hands on and the type of its counts are ADR-chain-loop-v2 D8.

D27 — Three words after Seq[T] say what the sequence is as a whole.

Beside the step’s sync, pauses and throws: ends - it can be walked from the back (a range, a list’s iter(), drain(), chars(), windows, chunks); sized - its length is known before it is walked (the same, and a map’s keys(), values(), drain(); not chars(), not a filter); replays - walking it does not use it up (only a range). None is in a program’s type grammar (ADR-105 D4): the ledger names them, the checker reads them, and a message never says them.

D28 — On a receiver the words are a demand; on a result they pass through.

Seq::rev writes its receiver Seq[$T] ends; a sequence without the word is NK2703, in the program’s words, with the way out (collect() it and walk the list from the back). A result has a word where its entry writes it and every sequence handed in has it. The ledger column ends_by_length = true says a result has a back end only where every input’s length is known - take, zip, skip, step_by.

D29 — A range is a sequence that replays, and below it is a value.

a..<b and a..b are Seq[T] sync ends sized replays, and NK2702 never applies to one. A range kept, handed on or called on is nikaia_std::range::Span (..<) or Through (..) below: Copy, its own iterator, a length for every integer type. A range written straight into a for or into brackets stays the language below’s a..b.

D30 — Every place a sequence is taken is seen, and a named one is walked by value.

Every read of a name that holds a sequence takes it - a let, an argument, a return, a field, every method - except an assignment’s target and a &; NK2702 refuses the next use. A sequence from outside a loop taken inside it, or taken inside a lambda, is refused where it is taken, unless the loop gives the name a new one or may leave on that turn. Two arms of one if or match are not one after the other. A for over a name that holds a sequence is handed it rather than lent it. Where in its statement a read stands, and what path it reads, are D41 and D42.

D31 — HashMap::insert is the write under the language below’s name.

(ref HashMap[$K, $V], key: $K, value: $V) -> $V?, keeping both. m[k] = v stays the language’s form; insert is for a program that wants the value it replaced. A text literal at a position the receiver binds to String is built into text of its own.

D32 — str::chars says what String::chars says.

Seq[char] sync ends.

D33 — A slice is ref Array[T], a run a function can take.

A range in a list’s brackets reads a run of type ref Array[T] (ADR-184 D3), &[T] below; a text slice is a view of text; a list’s windows and chunks hand out ref Array[$T]. A slice fits a parameter declared ref Array[T], as a list does; a ref Vec[T] parameter refuses one in this language’s words. A range kept in a name slices exactly as a written one, with no * (ADR-182 D2).

D34 — A copy of std’s is to_owned below.

.clone() of a &str or &[T] below clones the reference, so a clone the checker resolved to a std entry (String::clone, str::clone, Vec::clone) is written to_owned. A program’s own clone is untouched. That .to_owned() is not written by a program is ADR-282 D8’s.

D35 — collect() builds a list, or what the let, field or result it goes into declares.

Where nothing declares anything, a Vec. Where a let, a struct field or a function’s result declares a map the items fit as pairs, a set they fit, or String from characters or pieces of text, that is what it builds, with no list in between; below, the turbofish is left off. What the items do not fit is NK1103. An argument is not such a place. Where the place declares C[T]? and the items are T?, it builds the C[T] and is null at the first null item, which ends the walk (#1161).

D36 — Into a map whose key or value is mixed text, the pairs go through either_keys, either_values or either_pairs.

As a list’s items go through either_items() (ADR-282 D16). To decide it, the pass that decides text tiers reads what map makes as what its lambda hands back; filter, rev, skip, take and step_by hand on what they were given; a number, a truth value and null are no text of either kind.

4.4 — What is handed over

D37 — A key goes into a map as a key: handed over when written, lent when read.

For a map whose keys are owned, the checker tells the emitter per key what the brackets do, and index::at is not written. Written, the map keeps the key: a text literal is built into text where it stands (ADR-282 D2), a name is handed over (D39), a view is refused (NK1105) with ADR-282 D25’s sentence. Read, the key is lent (&name, &(k + 1)), or goes in as it is where it is a view already. A map keyed by views is unchanged.

D38 — A literal kept by an assignment is built there; a map’s text read with a literal after ?? is a view.

s = "b" and ids[k] = "seven" build the literal into a String, as a let, a field, a return and an argument do. m[k] ?? "-" over a map of text is a view of text (index::Or<&str> for Option<&String>); a place that keeps it as a String is told so in ADR-282’s words. So is m[k] ?? made() and m.get(k) ?? f"…", with text made in the fallback (#1322): the made text goes into a slot declared before the statement, filled only where the map holds nothing, as a value made beside a lent view does (#1256), and the answer is a view of text, (m.get(k) ?? zero()).clone() a String of its own.

D39 — Data used after it was handed to something that keeps it is NK2105.

The places a value is given away: an argument the callee keeps, a key or value written into a map or a list, a field of a struct being built (unless declared a view), an element of a list or tuple literal, a let that names it again, an assignment. A later read of what was taken is NK2105, with .clone() at the hand-over as the way out; a number, a bool, a char and a view copy and are not recorded. Loops, lambdas, the arms of one choice and an assignment between are as for a sequence (D30).

D40 — A branch that leaves the function takes its takings with it.

A walk or a hand-over inside an arm of an if or match, or a catch handler, whose last statement is a return or a throw is asked about reads up to the end of its statement and no further - for NK2105 and NK2702 alike. A break or continue leaves a branch, not the function, and is not this.

D41 — A read has a place in its statement.

Every read has an order in the walk, which is the order the language below evaluates: arguments, operands and struct fields left to right, a receiver before its arguments. A read later in the same statement is after a taking: keep(name, name) and s.count() + s.count() are refused. An assignment in the same statement gives back what it took: name = name + "x" is a program.

D42 — A read has a path, so a part can be handed over and the rest kept.

A taking of p.name touches a later read of p.name, of anything inside it, and of p; p.x is untouched. An assignment to p.name or p gives it back. A part of a value only lent here - a ref parameter or self, a for binding over a list, a let over a place - handed to something that keeps it is NK2106, naming p.name.clone(). A part handed to what keeps it keeps its parameter: fn f(p: P) { xs.push(p.name) } owns p; a field that copies keeps nothing, one whose type the file cannot see keeps.

D43 — A read through the brackets carries no parentheses of its own.

*index::get(…) is written bare; every postfix is written through one function (postfix_base), which adds them where (*get(…)).len() needs them. So rustc warns about nothing in a read.

5. Consequences

The rules a program follows are Part I 3.3 (ranges and for), 4.5 (maps, lists) and 6.5 (what is handed over), and Part III 13.5 (Seq’s words, collect), A.2 and C.3.

6. What this does not decide

This site is open source. Improve this page.