rust-iterators-closures

Refactor Rust code to use idiomatic iterator and closure patterns.

Updated May 26, 2026
One-click install
npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill rust-iterators-closures
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-iterators-closures
Source: https://github.com/adelabdelgawad/rust-fullstack-agents/tree/main/plugins/rusty/skills/rust-iterators-closures
Command: npx skills add https://github.com/adelabdelgawad/rust-fullstack-agents --skill rust-iterators-closures

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Canonical reference for iterator and closure idioms in Rust. Detects and fixes: C-style index loops where an iterator adapter fits, unused lazy iterators (map/filter with no terminal), needless intermediate Vec from chained collect+loop, .clone() inside .map() to dodge a borrow, unwrap() inside map/filter instead of collect::<Result<,>>(), oversized move closures capturing large structs by value, and FnOnce/FnMut/Fn bound mismatches (E0525). Covers: iterator laziness, Iterator trait + next, consuming vs non-consuming adapters (map, filter, fold, collect, zip, enumerate), turbofish on collect(), collecting into Result, iter vs iter_mut vs into_iter, Fn/FnMut/FnOnce traits and when each is required, move closures for spawned tasks and 'static bounds, borrow-not-clone discipline, and zero-cost abstraction.

Core Features & Use Cases

  • Replace C-style index loops with iterator adapters
  • Use correct iterator ownership (iter/iter_mut/into_iter) and consuming adapters
  • Detect and avoid common pitfalls (unwrap/expect inside closures, .clone() in map, lazy iterators)

Quick Start

Refactor target loops and closures into idiomatic Rust iterator chains and closures.

Frequently Asked Questions about rust-iterators-closures

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor C-style index loops into idiomatic Rust iterators?

Choose iter for immutable borrowing, iter_mut for mutable borrowing, and into_iter to consume the collection. Selecting the correct iterator ownership prevents unnecessary clones and ensures proper borrow-not-clone discipline within your map and filter chains.

How do I collect Rust iterator results into a Result instead of using unwrap in map?

Collect iterator results into a Result by using collect::<Result<_, _>>() instead of unwrap inside map. This avoids anti-patterns, properly handles fallible operations within closures, and cleanly propagates errors without panicking.

When should I use move closures with FnOnce, FnMut, and Fn bounds in Rust?

Use move closures to capture variables by value for spawned tasks requiring 'static bounds. FnOnce consumes captured variables, FnMut mutably borrows them, and Fn borrows immutably, preventing E0525 bound mismatches.

Why are my Rust map and filter chains not executing?

Map and filter chains are lazy iterators that do not execute without a terminal consuming adapter. You must consume the chain using methods like collect, fold, or a for loop to trigger evaluation and avoid unused lazy iterators.

What is the best way to avoid unnecessary clones inside Rust map closures?

Avoid unnecessary clones inside map closures by enforcing borrow-not-clone discipline and selecting correct iterator ownership. Using iter, iter_mut, or into_iter appropriately allows direct access without dodging borrows through expensive .clone() calls.