File tree Expand file tree Collapse file tree 5 files changed +115
-0
lines changed Expand file tree Collapse file tree 5 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -18,4 +18,5 @@ members = [
18
18
" structural/proxy" ,
19
19
" behavioral/chain-of-responsibility" ,
20
20
" behavioral/command" ,
21
+ " behavioral/iterator" ,
21
22
]
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ edition = " 2021"
3
+ name = " iterator"
4
+ version = " 0.1.0"
5
+
6
+ [[bin ]]
7
+ name = " iterator"
8
+ path = " main.rs"
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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 \n Let'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 ! ( "\n All elements in user collection: " ) ;
25
+ users. iter ( ) . for_each ( |e| print ! ( "{} " , e) ) ;
26
+
27
+ println ! ( ) ;
28
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments