Rust Iterator Collections
Use this skill to turn collection code into clear, idiomatic Rust. Prefer
iterator and collection APIs that express the operation directly while keeping
ownership, allocation, and error behavior visible.
Core Workflow
- Identify the input ownership mode: borrowed iteration, mutable iteration, or
consuming iteration.
- Pick the simplest iterator shape:
iter, iter_mut, into_iter, ranges,
drain, or a collection-specific method.
- Replace manual loops with adapters only when the resulting code is clearer.
Keep explicit loops for complex branching, early mutation, or debugging.
- Use fallible iterator consumers such as
collect::<Result<Vec<_>, _>>(),
try_fold, and try_for_each when errors should short-circuit.
- Use
HashMap::entry or BTreeMap::entry for insert-or-update logic.
- Avoid unnecessary intermediate
Vecs. Chain iterators or extend an existing
collection when possible.
- Test empty input, single item input, duplicate keys, ordering expectations,
and error short-circuiting.
Iterator Rules
- Accept
impl IntoIterator<Item = T> when the function only needs iteration.
- Use
&[T] when slice semantics are enough; avoid &Vec<T>.
- Use
filter_map for map-then-discard-None; use flat_map when each item
expands to zero or more items.
- Use
map_while or scan for stateful transformations when they make stopping
behavior explicit.
- Add type annotations at
collect boundaries, not throughout the pipeline.
- Prefer
cloned or copied over map(|x| x.clone()) when cloning iterator
items is intentional.
Collection Rules
Read references/iterator-collection-patterns.md when refactoring a loop or
reviewing collection mutation.
- Use
Vec for contiguous ordered data, VecDeque for queue-like push/pop at
both ends, BinaryHeap for priority queues, HashMap for unordered lookup,
and BTreeMap for sorted lookup or range queries.
- Use
retain, drain, splice, and split_off instead of mutating a
collection while separately iterating over borrowed elements.
- Use
Entry::or_insert_with when default construction is expensive.
- Preserve ordering deliberately. Do not swap
BTreeMap for HashMap when
iteration order is part of behavior.
- Reserve capacity only when size is known or profiling shows reallocation
matters.
Review Checklist
- The pipeline communicates ownership and error behavior clearly.
- No
contains_key followed by insert where entry would be simpler.
- No avoidable
collect::<Vec<_>>() just to iterate again.
- Indexing is used only when indices are the domain concept.
- Tests cover duplicates, empty input, and deterministic ordering where needed.
Source: hashgraph-online/awesome-codex-plugins → plugins/LVTD-LLC/skills/skills/rust-iterator-collections/SKILL.md
1---2name: rust-iterator-collections3description: Design and review Rust iterator pipelines and collection code for clear ownership, allocation, ordering, and error behavior. Use when writing, refactoring, or reviewing iterators, HashMap Entry usage, collect, FromIterator, Extend, custom iterators, or allocation-heavy loops.4---567# Rust Iterator Collections89Use this skill to turn collection code into clear, idiomatic Rust. Prefer10iterator and collection APIs that express the operation directly while keeping11ownership, allocation, and error behavior visible.1213## Core Workflow14151. Identify the input ownership mode: borrowed iteration, mutable iteration, or16 consuming iteration.172. Pick the simplest iterator shape: `iter`, `iter_mut`, `into_iter`, ranges,18 `drain`, or a collection-specific method.193. Replace manual loops with adapters only when the resulting code is clearer.20 Keep explicit loops for complex branching, early mutation, or debugging.214. Use fallible iterator consumers such as `collect::<Result<Vec<_>, _>>()`,22 `try_fold`, and `try_for_each` when errors should short-circuit.235. Use `HashMap::entry` or `BTreeMap::entry` for insert-or-update logic.246. Avoid unnecessary intermediate `Vec`s. Chain iterators or extend an existing25 collection when possible.267. Test empty input, single item input, duplicate keys, ordering expectations,27 and error short-circuiting.2829## Iterator Rules3031- Accept `impl IntoIterator<Item = T>` when the function only needs iteration.32- Use `&[T]` when slice semantics are enough; avoid `&Vec<T>`.33- Use `filter_map` for map-then-discard-`None`; use `flat_map` when each item34 expands to zero or more items.35- Use `map_while` or `scan` for stateful transformations when they make stopping36 behavior explicit.37- Add type annotations at `collect` boundaries, not throughout the pipeline.38- Prefer `cloned` or `copied` over `map(|x| x.clone())` when cloning iterator39 items is intentional.4041## Collection Rules4243Read `references/iterator-collection-patterns.md` when refactoring a loop or44reviewing collection mutation.4546- Use `Vec` for contiguous ordered data, `VecDeque` for queue-like push/pop at47 both ends, `BinaryHeap` for priority queues, `HashMap` for unordered lookup,48 and `BTreeMap` for sorted lookup or range queries.49- Use `retain`, `drain`, `splice`, and `split_off` instead of mutating a50 collection while separately iterating over borrowed elements.51- Use `Entry::or_insert_with` when default construction is expensive.52- Preserve ordering deliberately. Do not swap `BTreeMap` for `HashMap` when53 iteration order is part of behavior.54- Reserve capacity only when size is known or profiling shows reallocation55 matters.5657## Review Checklist5859- The pipeline communicates ownership and error behavior clearly.60- No `contains_key` followed by `insert` where `entry` would be simpler.61- No avoidable `collect::<Vec<_>>()` just to iterate again.62- Indexing is used only when indices are the domain concept.63- Tests cover duplicates, empty input, and deterministic ordering where needed.6465---6667**Source:** [`hashgraph-online/awesome-codex-plugins`](https://github.com/hashgraph-online/awesome-codex-plugins) → `plugins/LVTD-LLC/skills/skills/rust-iterator-collections/SKILL.md`