22
33[ 中文README] ( https://github.com/Natural-selection1/better-comprehension-in-rust/blob/master/README-CN.md )
44
5- Collection comprehension and Iterator comprehension in Rust. And it provides a better experience in Rust.
5+ Collection comprehension and Iterator comprehension in Rust.
6+ And it provides a better experience in Rust.
67
78# Usage
89
9- The syntax is derived from [ Python's comprehension] ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ) .
10+ The syntax is derived from [ Python's comprehension]
11+ (https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ).
1012
11- This library provides macros for all collection types in the Rust standard library and an Iterator based on references.
13+ This library provides macros for all collection types
14+ in the Rust standard library and an Iterator based on references.
1215
1316---
1417simple example
@@ -55,18 +58,22 @@ let binary_heap = binary_heap![
5558assert_eq! (binary_heap . into_sorted_vec (), vec! [1 , 1 , 3 , 13 ]);
5659```
5760---
58- the reading order of the for loop in this library is from top to bottom, just like Python's comprehension.
61+ the reading order of the for loop in this library is from top to bottom,
62+ just like Python's comprehension.
5963``` rust
6064let vec = vector! [
6165 (top ,bottom )
6266 for top in 1 ..= 3 if top != 2
6367 for bottom in 4 ..= 6 if bottom + top != 4 ];
64- assert_eq! (vec , vec! [(1 , 4 ), (1 , 5 ), (1 , 6 ), (3 , 4 ), (3 , 5 ), (3 , 6 )]);
68+ assert_eq! (vec , vec! [(1 , 4 ), (1 , 5 ), (1 , 6 ),
69+ (3 , 4 ), (3 , 5 ), (3 , 6 )]);
6570```
6671---
6772
6873Note that in Rust, for loops consume ownership.
69- So typically, for nested loops, if you want the original container to be consumed, you should write it like this:
74+ So typically, for nested loops,
75+ if you want the original container to be consumed,
76+ you should write it like this:
7077
7178``` rust
7279let vec_1 = vec! [" ABC" . to_string (), " DEF" . to_string ()];
@@ -109,7 +116,8 @@ But in this library, you don't need to do this,
109116
110117the provided macros will automatically handle these problems for you.
111118
112- You only need to add .iter() or use & before the variable you want to keep ownership,
119+ You only need to add .iter()
120+ or use & before the variable you want to keep ownership,
113121
114122the rest will be automatically handled in the macro.
115123``` rust
@@ -130,39 +138,52 @@ println!("{:?}", vec_2); // work well
130138---
131139This library also supports key-value collection types, HashMap, BTreeMap
132140``` rust
133- let vec_key = vec! [" key_1" . to_string (), " key_2" . to_string (), " key_3" . to_string ()];
134- let vec_value = [1 , 2 , 3 ];
135- let hash_map = hash_map! {
136- // the following three key-value pair separators are supported
137- key . clone () : * value
138- // key.clone() => *value
139- // key.clone() , *value
140- for key in vec_key
141- for value in vec_value
142- };
143- assert_eq! (
144- hash_map ,
145- HashMap :: from ([
146- (" key_1" . to_string (), 3 ),
147- (" key_2" . to_string (), 3 ),
148- (" key_3" . to_string (), 3 )
149- ])
150- );
141+ let vec_key = vec! [" key_1" . to_string (),
142+ " key_2" . to_string (),
143+ " key_3" . to_string ()];
144+ let vec_value = [1 , 2 , 3 ];
145+
146+ let hash_map = hash_map! {
147+ // the following three key-value pair separators are supported
148+ key . clone () : * value
149+ // key.clone() => *value
150+ // key.clone() , *value
151+ for key in vec_key
152+ for value in vec_value
153+ };
154+ assert_eq! (
155+ hash_map ,
156+ HashMap :: from ([
157+ (" key_1" . to_string (), 3 ),
158+ (" key_2" . to_string (), 3 ),
159+ (" key_3" . to_string (), 3 )
160+ ])
161+ );
151162```
152163---
153- Iterator comprehension is also supported,but unlike the collection comprehension above,
164+ Iterator comprehension is also supported,
165+ but unlike the collection comprehension above,
154166
155- this iterator comprehension is based on references,so it will not consume ownership.
167+ this iterator comprehension is based on references,
168+ so it will not consume ownership.
156169``` rust
157- let vec_1 = [" 123" . to_string (), " 456" . to_string (), " 789" . to_string ()];
158- let vec_2 = [" ABC" . to_string (), " DEF" . to_string (), " GHI" . to_string ()];
170+ let vec_1 = [" 123" . to_string (),
171+ " 456" . to_string (),
172+ " 789" . to_string ()];
173+ let vec_2 = [" ABC" . to_string (),
174+ " DEF" . to_string (),
175+ " GHI" . to_string ()];
159176
160177let mut result3 = iterator_ref! [
161178(x . clone (), y . clone ()) if x . contains (" 1" ) else (y . clone (), x . clone ())
162179for x in vec_1 if x . contains (" 1" ) || x . contains (" 7" )
163180for i in 1 ..= 2
164181for y in vec_2 if y . contains (" D" ) || x . contains (" 3" )];
165182
183+ // still alive even there is no & or .iter()
184+ println! (" {:?}" , vec_1 );
185+ println! (" {:?}" , vec_2 );
186+
166187for _ in 0 ..= 9 {
167188 println! (" {:?}" , result3 . next ());
168189}
@@ -183,8 +204,13 @@ None
183204
184205The above writing is equivalent to the following writing
185206``` rust
186- let vec_1 = [" 123" . to_string (), " 456" . to_string (), " 789" . to_string ()];
187- let vec_2 = [" ABC" . to_string (), " DEF" . to_string (), " GHI" . to_string ()];
207+ let vec_1 = [" 123" . to_string (),
208+ " 456" . to_string (),
209+ " 789" . to_string ()];
210+ let vec_2 = [" ABC" . to_string (),
211+ " DEF" . to_string (),
212+ " GHI" . to_string ()];
213+
188214let mut result3 = {
189215 let vec_2 = vec_2 . iter (). collect :: <Vec <_ >>();
190216 let vec_1 = vec_1 . iter (). collect :: <Vec <_ >>();
0 commit comments