Adapters and Consumers
Chaining operations.
Adapters and Consumers is a free Learn Rust Coding lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Kinds of Methods
Iterator methods fall into two groups:
- Adapters return a new iterator and are lazy (
map,filter,take) - Consumers run the iterator and produce a final value (
sum,collect,for_each)
Every pipeline is zero or more adapters followed by exactly one consumer.
take: Limit the Count
take(n) yields at most n items, then stops. It is an adapter, so it stays lazy.
It works even on infinite iterators, taking only what you ask for.
fn main() {
let first_three: Vec<i32> = (1..).take(3).collect();
println!("{:?}", first_three);
}skip: Ignore the First Items
skip(n) drops the first n items and yields the rest. Combine it with take to grab a window from the middle.
fn main() {
let window: Vec<i32> = (1..=10).skip(3).take(4).collect();
println!("{:?}", window);
}enumerate: Pair With an Index
enumerate yields (index, value) tuples, counting from zero. It is the idiomatic way to get positions without a manual counter.
fn main() {
let letters = vec!['a', 'b', 'c'];
for (i, c) in letters.iter().enumerate() {
println!("{} -> {}", i, c);
}
}zip: Walk Two Iterators Together
zip pairs items from two iterators. It stops when the shorter one runs out.
This is handy for combining parallel data such as names and scores.
fn main() {
let names = vec!["Ana", "Ben"];
let ages = vec![30, 25];
for (name, age) in names.iter().zip(ages.iter()) {
println!("{} is {}", name, age);
}
}rev: Reverse the Order
rev yields items back to front. It requires a double-ended iterator, which ranges and slices provide.
fn main() {
let backwards: Vec<i32> = (1..=5).rev().collect();
println!("{:?}", backwards);
}chain: Join Two Iterators
chain runs the first iterator to completion, then continues with the second, as if they were one sequence.
fn main() {
let joined: Vec<i32> = (1..=3).chain(10..=12).collect();
println!("{:?}", joined);
}fold: Build a Single Value
fold is the general consumer. It starts with an accumulator and folds each item into it using a closure.
Many consumers like sum are special cases of fold.
fn main() {
let product = (1..=5).fold(1, |acc, x| acc * x);
println!("5! = {}", product);
}any and all: Boolean Consumers
any returns true if at least one item satisfies the predicate; all returns true only if every item does.
Both short-circuit: they stop as soon as the answer is known.
fn main() {
let nums = vec![2, 4, 6, 7];
println!("{}", nums.iter().all(|x| x % 2 == 0));
println!("{}", nums.iter().any(|x| x % 2 != 0));
}find and position
find returns the first item matching a predicate as an Option. position returns the index of the first match instead.
Both short-circuit on the first hit.
fn main() {
let nums = vec![1, 3, 5, 8, 9];
println!("{:?}", nums.iter().find(|&&x| x % 2 == 0));
println!("{:?}", nums.iter().position(|&x| x % 2 == 0));
}Designing Pipelines
Build pipelines by stacking adapters, then ending with one consumer. Keep them readable: one operation per line.
Because adapters are lazy, even long chains do only one pass over the data.
Quick Check
Adapters versus consumers.
Recap
You explored chaining operations:
- Adapters (
take,skip,enumerate,zip,rev,chain) stay lazy - Consumers (
fold,any,all,find,position) finish the work - Boolean and search consumers short-circuit
- Every pipeline is adapters then one consumer
Frequently asked questions
Is the “Adapters and Consumers” lesson free?
Yes — the full text of “Adapters and Consumers” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Adapters and Consumers”?
Chaining operations. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Adapters and Consumers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Iterator Trait
- map, filter, collect
- Adapters and Consumers
- Custom Iterators