Skip to content

Commit 9e5a9a3

Browse files
committed
Iterator pattern: starnard and custom
1 parent 5f6b5ec commit 9e5a9a3

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ members = [
1818
"structural/proxy",
1919
"behavioral/chain-of-responsibility",
2020
"behavioral/command",
21+
"behavioral/iterator",
2122
]

behavioral/iterator/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
edition = "2021"
3+
name = "iterator"
4+
version = "0.1.0"
5+
6+
[[bin]]
7+
name = "iterator"
8+
path = "main.rs"

behavioral/iterator/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Iterator
2+
3+
Iterator is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details.
4+
5+
## Standard Iterator
6+
7+
```rust
8+
let array = &[1, 2, 3];
9+
let iterator = array.iter();
10+
11+
// Traversal over each element of the vector.
12+
iterator.for_each(|e| print!("{}, ", e));
13+
```
14+
15+
## Custom Iterator
16+
17+
```rust
18+
let users = UserCollection::new();
19+
let mut iterator = users.iter();
20+
21+
iterator.next();
22+
```
23+
24+
## Execution Result
25+
26+
```
27+
Iterators are widely used in the standard library: 1 2 3
28+
29+
Let's test our own iterator.
30+
31+
1nd element: Some("Alice")
32+
2nd element: Some("Bob")
33+
3rd element: Some("Carl")
34+
4th element: None
35+
36+
37+
All elements in user collection: Alice Bob Carl
38+
```

behavioral/iterator/main.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::users::UserCollection;
2+
3+
mod users;
4+
5+
fn main() {
6+
print!("Iterators are widely used in the standard library: ");
7+
8+
let array = &[1, 2, 3];
9+
let iterator = array.iter();
10+
11+
// Traversal over each element of the array.
12+
iterator.for_each(|e| print!("{} ", e));
13+
14+
println!("\n\nLet's test our own iterator.\n");
15+
16+
let users = UserCollection::new();
17+
let mut iterator = users.iter();
18+
19+
println!("1nd element: {:?}", iterator.next());
20+
println!("2nd element: {:?}", iterator.next());
21+
println!("3rd element: {:?}", iterator.next());
22+
println!("4th element: {:?}", iterator.next());
23+
24+
print!("\nAll elements in user collection: ");
25+
users.iter().for_each(|e| print!("{} ", e));
26+
27+
println!();
28+
}

behavioral/iterator/users.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pub struct UserCollection {
2+
users: [&'static str; 3],
3+
}
4+
5+
impl UserCollection {
6+
pub fn new() -> Self {
7+
Self {
8+
users: ["Alice", "Bob", "Carl"],
9+
}
10+
}
11+
12+
pub fn iter(&self) -> UserIterator {
13+
UserIterator {
14+
index: 0,
15+
user_collection: self,
16+
}
17+
}
18+
}
19+
20+
/// UserIterator allows sequential traversal through a complex users collection
21+
/// without exposing its internal details.
22+
pub struct UserIterator<'a> {
23+
index: usize,
24+
user_collection: &'a UserCollection,
25+
}
26+
27+
// `Iterator` is a standard interface for dealing with iterators from the Rust standard library.
28+
impl Iterator for UserIterator<'_> {
29+
type Item = &'static str;
30+
31+
fn next(&mut self) -> Option<Self::Item> {
32+
if self.index < self.user_collection.users.len() {
33+
let user = Some(self.user_collection.users[self.index]);
34+
self.index += 1;
35+
return user;
36+
}
37+
38+
None
39+
}
40+
}

0 commit comments

Comments
 (0)