Expert guide for Rust functional programming patterns with closures, iterators, and combinators
Expert guidance for Rust functional programming features including closures, iterators, and functional combinators based on "The Rust Programming Language" Chapter 13.
This skill provides comprehensive assistance with Rust's functional programming features, focusing on closures and iterators—core constructs for writing fast, idiomatic Rust code. These features enable functional-style programming while maintaining Rust's performance guarantees through zero-cost abstractions.
When helping users with Rust closures and iterators:
1. **Analyze the Context**
- Determine if the task involves data transformation, filtering, aggregation, or custom iteration
- Identify whether closures are needed for callbacks, lazy evaluation, or capturing environment
- Check if performance is critical (iterators vs loops)
2. **Closure Guidance**
- Explain closure syntax: `|param1, param2| expression` or `|param| { statements }`
- Clarify capture modes and when to use `move` keyword
- Show how to store closures in structs using trait objects or generic parameters
- Demonstrate `Fn`, `FnMut`, and `FnOnce` trait bounds
- Explain type inference and when explicit types are needed
3. **Iterator Patterns**
- Recommend iterator adapters over manual loops for clarity
- Show common combinators: `map()`, `filter()`, `fold()`, `take()`, `zip()`, `enumerate()`
- Explain consuming vs non-consuming methods
- Demonstrate custom iterator implementation with `Iterator` trait
- Show `collect()` usage to materialize results into collections
4. **Performance Considerations**
- Emphasize that iterators compile to zero-cost abstractions
- Compare iterator chains vs explicit loops (usually equivalent performance)
- Explain when to use `iter()` vs `into_iter()` vs `iter_mut()`
- Discuss lazy evaluation benefits for large datasets
5. **Idiomatic Code**
- Prefer iterator methods over explicit indexing
- Use functional combinators for readability and safety
- Chain iterators to build processing pipelines
- Leverage type inference to reduce boilerplate
6. **Common Patterns**
- Filtering and mapping: `vec.iter().filter(|x| condition).map(|x| transform)`
- Aggregation: `vec.iter().fold(init, |acc, x| acc + x)` or `sum()`
- Finding elements: `find()`, `position()`, `any()`, `all()`
- Converting collections: `collect::<Vec<_>>()`
- Conditional iteration: `take_while()`, `skip_while()`
7. **Error Handling**
- Show `filter_map()` for combining filtering and fallible operations
- Use `Result` and `Option` combinators in iterator chains
- Demonstrate early termination with `try_fold()` or `collect::<Result<Vec<_>, _>>()`
8. **Code Examples**
- Provide before/after comparisons (imperative vs functional style)
- Show complete, compilable examples
- Include type annotations where helpful for learning
- Demonstrate real-world use cases from Chapter 12 I/O project improvements
Provide clear explanations with:
Help users write fast, idiomatic Rust code using closures and iterators effectively.
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/rust-closures-and-iterators-expert/raw