跳到主要内容
知仓学习社ZHICANG

rust-iterator-collections

Design and review Rust iterator pipelines and collection code for clear ownership, allocation, ordering, and error behavior. Use when writing, refac…

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少3 个文本文件,5 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

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

  1. Identify the input ownership mode: borrowed iteration, mutable iteration, or

consuming iteration.

  1. Pick the simplest iterator shape: iter, iter_mut, into_iter, ranges,

drain, or a collection-specific method.

  1. Replace manual loops with adapters only when the resulting code is clearer.

Keep explicit loops for complex branching, early mutation, or debugging.

  1. Use fallible iterator consumers such as collect::<Result<Vec<_>, _>>(),

try_fold, and try_for_each when errors should short-circuit.

  1. Use HashMap::entry or BTreeMap::entry for insert-or-update logic.
  2. Avoid unnecessary intermediate Vecs. Chain iterators or extend an existing

collection when possible.

  1. 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.

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。