Rust Iterator Collections
Developmentrust-iterator-collectionsRust Iterator Collections is an Agent Skill for development published in the hashgraph-online/awesome-codex-plugins repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
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.
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Inspect the Agent Skill at https://github.com/hashgraph-online/awesome-codex-plugins/blob/HEAD/plugins/LVTD-LLC/skills/skills/rust-iterator-collections/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/rust-iterator-collections, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
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, andtry_for_eachwhen errors should short-circuit. - Use
HashMap::entryorBTreeMap::entryfor 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_mapfor map-then-discard-None; useflat_mapwhen each item expands to zero or more items. - Use
map_whileorscanfor stateful transformations when they make stopping behavior explicit. - Add type annotations at
collectboundaries, not throughout the pipeline. - Prefer
clonedorcopiedovermap(|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
Vecfor contiguous ordered data,VecDequefor queue-like push/pop at both ends,BinaryHeapfor priority queues,HashMapfor unordered lookup, andBTreeMapfor sorted lookup or range queries. - Use
retain,drain,splice, andsplit_offinstead of mutating a collection while separately iterating over borrowed elements. - Use
Entry::or_insert_withwhen default construction is expensive. - Preserve ordering deliberately. Do not swap
BTreeMapforHashMapwhen 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_keyfollowed byinsertwhereentrywould 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.