11# better_comprehension
22
3- 在rust中的集合推导式和迭代器推导式。提供更好的Rust使用体验
3+ [ 中文README ] ( https://github.com/Natural-selection1/better-comprehension-in-rust/blob/master/README-CN.md )
44
55Collection comprehension and Iterator comprehension in Rust. And it provides a better experience in Rust.
66
77# Usage
8- 语法源自[ python推导式] ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ) 。
9- 本库为Rust标准库中的所有集合类型提供宏,以及基于引用的迭代器
108
119The syntax is derived from [ Python's comprehension] ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ) .
1210
1311This library provides macros for all collection types in the Rust standard library and an Iterator based on references.
1412
1513---
16- 简单示例
1714simple example
1815``` rust
1916let vec_1 = vec! [" AB" . to_string (), " CD" . to_string ()];
@@ -22,8 +19,6 @@ assert_eq!(vec, vec!["AB".to_string(), "CD".to_string()]);
2219```
2320
2421---
25- 你也可以在推导式中使用模式
26-
2722You can also use patterns in it
2823``` rust
2924struct Person {
@@ -36,16 +31,12 @@ let vec_deque = vec_deque![name.clone() for Person { name, .. } in people];
3631assert_eq! (vec_deque , VecDeque :: from ([" Joe" . to_string (), " Bob" . to_string ()]));
3732```
3833---
39- 过滤值
40-
41- filtering values
34+ filtering values before comprehension
4235``` rust
4336let linked_list = linked_list! [ i * 2 for i in 1 ..= 3 if i != 2 ];
4437assert_eq! (linked_list , LinkedList :: from ([2 , 6 ]));
4538```
4639---
47- 根据条件返回不同的值
48-
4940return different values based on conditions
5041``` rust
5142let b_tree_set = b_tree_set! {
@@ -55,8 +46,6 @@ let b_tree_set = b_tree_set!{
5546assert_eq! (b_tree_set , BTreeSet :: from ([1 , 13 ]));
5647```
5748---
58- 嵌套推导式
59-
6049nested comprehension
6150``` rust
6251let binary_heap = binary_heap! [
@@ -66,8 +55,6 @@ let binary_heap = binary_heap![
6655assert_eq! (binary_heap . into_sorted_vec (), vec! [1 , 1 , 3 , 13 ]);
6756```
6857---
69- 和python的推导式一样, 本库的for循环是从上到下读取的.
70-
7158the reading order of the for loop in this library is from top to bottom, just like Python's comprehension.
7259``` rust
7360let vec = vector! [
@@ -76,10 +63,7 @@ let vec = vector![
7663 for bottom in 4 ..= 6 if bottom + top != 4 ];
7764assert_eq! (vec , vec! [(1 , 4 ), (1 , 5 ), (1 , 6 ), (3 , 4 ), (3 , 5 ), (3 , 6 )]);
7865```
79-
80- 需要注意的是, 由于在rust中, for loop 是消耗所有权的.
81-
82- 所以通常来说, 对于多层循环, 如果你希望原容器被消耗, 你应该写成如下这样:
66+ ---
8367
8468Note that in Rust, for loops consume ownership.
8569So typically, for nested loops, if you want the original container to be consumed, you should write it like this:
@@ -89,28 +73,24 @@ let vec_1 = vec!["ABC".to_string(), "DEF".to_string()];
8973let vec_2 = vec! [" abc" . to_string (), " def" . to_string ()];
9074let vec_3 = vec! [123 , 456 ];
9175let vec = {
92- // 遮蔽想消耗的变量
9376 // shadow the variable you want to consume
9477 let vec_1 = vec_1 ;
9578 let vec_3 = vec_3 ;
9679
9780 let mut vec = vec! [];
98- // 在外层循环里, 你可以选择使用iter()保留所有权
99- // In the outer loop, you can choose to use iter() to keep ownership
10081 for i in vec_1 . iter () {
10182 if i == " ABC" {
102- // 在内层循环里, 你必须使用iter() , 否则所有权会在第一次被转移
10383 // In the inner loop, you must use iter(),
10484 // otherwise ownership will be transferred for the first time
10585 for j in vec_2 . iter () {
10686 if j == " abc" {
107- // 如果不使用iter(), 那么vec_3的所有权会在第一次被转移
10887 // If you do not use iter(),
109- // then the ownership of vec_3 will be transferred for the first time
88+ // then the ownership of vec_3
89+ // will be transferred for the first time
11090 for k in vec_3 . iter () {
11191 if k == & 123 {
112- // 仅在必要时使用clone, 以避免不必要的资源浪费
113- // Only use clone when necessary to avoid unnecessary resource waste
92+ // Only use clone when necessary
93+ // to avoid unnecessary resource waste
11494 vec . push ((i . clone (), j . clone (), * k ));
11595 }
11696 }
@@ -124,14 +104,14 @@ let vec = {
124104println! (" {:?}" , vec_2 ); // work well
125105// println!("{:?}", vec_3); // borrow of moved value
126106```
107+ ---
108+ But in this library, you don't need to do this,
127109
128- 但在本库中, 你不需要这么做, 提供的宏会自动帮你处理这些问题.
129-
130- But in this library, you don't need to do this, the provided macros will automatically handle these problems for you.
110+ the provided macros will automatically handle these problems for you.
131111
132- 你唯一需要做的就是在你想要保留所有权的变量后面加上 .iter() 或 使用 & , 其余会在宏内自动处理.
112+ You only need to add .iter() or use & before the variable you want to keep ownership,
133113
134- You only need to add .iter() or use & before the variable you want to keep ownership, the rest will be automatically handled in the macro.
114+ the rest will be automatically handled in the macro.
135115``` rust
136116let vec_1 = vec! [" ABC" . to_string (), " DEF" . to_string ()];
137117let vec_2 = vec! [" abc" . to_string (), " def" . to_string ()];
@@ -140,21 +120,21 @@ let vec = vector![
140120 (i . clone (),j . clone (),* k )
141121 for i in vec_1 if i == " ABC"
142122 for j in vec_2 . iter () if j == " abc"
143- // for j in &vec_2 if j == "abc" 这种写法也是可以的 this is also reasonable
123+ // for j in &vec_2 if j == "abc" // this is also reasonable
144124 for k in vec_3 if k == & 123
145125];
146126// println!("{:?}", vec_1); // borrow of moved value
147127println! (" {:?}" , vec_2 ); // work well
148128// println!("{:?}", vec_3); // borrow of moved value
149129```
150-
151- 同时, 该库还支持键值对容器类型, HashMap, BTreeMap
130+ ---
152131This library also supports key-value collection types, HashMap, BTreeMap
153132``` rust
154133let vec_key = vec! [" key_1" . to_string (), " key_2" . to_string (), " key_3" . to_string ()];
155134 let vec_value = [1 , 2 , 3 ];
156135 let hash_map = hash_map! {
157- key . clone () : * value // 三种键值对分隔符都支持
136+ // the following three key-value pair separators are supported
137+ key . clone () : * value
158138 // key.clone() => *value
159139 // key.clone() , *value
160140 for key in vec_key
@@ -169,13 +149,7 @@ let vec_key = vec!["key_1".to_string(), "key_2".to_string(), "key_3".to_string()
169149 ])
170150 );
171151```
172-
173- 该库也支持迭代器推导式, 但不同于上面的集合推导式
174-
175- 该迭代器推导式是基于引用的, 所以不会消耗所有权
176-
177- 除此之外的写法与集合推导式完全相同
178-
152+ ---
179153Iterator comprehension is also supported,but unlike the collection comprehension above,
180154
181155this iterator comprehension is based on references,so it will not consume ownership.
206180*/
207181```
208182
209- 以上写法与下面的写法是完全的等价形式
210183
211184The above writing is equivalent to the following writing
212185``` rust
@@ -216,33 +189,23 @@ let vec_2 = ["ABC".to_string(), "DEF".to_string(), "GHI".to_string()];
216189let mut result3 = {
217190 let vec_2 = vec_2 . iter (). collect :: <Vec <_ >>();
218191 let vec_1 = vec_1 . iter (). collect :: <Vec <_ >>();
219- (vec_1 )
220- . into_iter ()
221- . filter_map ( move | x | {
222- (x . contains ( " 1 " ) || x . contains ( " 7 " ))
223- . then (|| {
192+ (vec_1 ). into_iter () . filter_map ( move | x | {
193+ ( x . contains ( " 1 " ) || x . contains ( " 7 " )) . then ( || {
194+ let vec_2 = vec_2 . clone ();
195+ (1 ..= 2 ) . into_iter () . filter_map ( move | i | {
196+ ( true ) . then (|| {
224197 let vec_2 = vec_2 . clone ();
225- (1 ..= 2 )
226- . into_iter ()
227- . filter_map (move | i | {
228- (true )
229- . then (|| {
230- let vec_2 = vec_2 . clone ();
231- (vec_2 )
232- . into_iter ()
233- . filter_map (move | y | {
234- (y . contains (" D" ) || x . contains (" 3" ))
235- . then (|| {
236- if x . contains (" 1" ) {
237- (x . clone (), y . clone ())
238- } else {
239- (y . clone (), x . clone ())
240- }
241- })
242- })
243- })
198+ (vec_2 ). into_iter (). filter_map (move | y | {
199+ (y . contains (" D" ) || x . contains (" 3" )). then (|| {
200+ if x . contains (" 1" ) {
201+ (x . clone (), y . clone ())
202+ } else {
203+ (y . clone (), x . clone ())
204+ }
244205 })
206+ })
245207 })
208+ })
246209 })
247210 . flatten ()
248211 . flatten ()
@@ -252,50 +215,18 @@ let mut result3 = {
252215
253216# some details
254217
255- vector! :
256-
257- 使用push添加元素
258-
259- use push to add elements
260-
261- vec_deque! :
262-
263- 使用push_back添加元素
264-
265- use push_back to add elements
266-
267- linked_list! :
268-
269- 使用push_back添加元素
270-
271- use push_back to add elements
272-
273- hash_set! :
274-
275- 使用insert添加元素
276-
277- use insert to add elements
278-
279- hash_map! :
280-
281- 使用insert添加键值对
282-
283- use insert to add key-value pairs
284-
285- b_tree_map! :
286-
287- 使用insert添加键值对
218+ vector! : push() to add elements
288219
289- use insert to add key-value pairs
220+ vec_deque! : push_back() to add elements
290221
291- b_tree_set ! :
222+ linked_list ! : push_back() to add elements
292223
293- 使用insert添加元素
224+ binary_heap! : push() to add elements
294225
295- use insert to add elements
226+ hash_set! : insert() to add elements
296227
297- binary_heap ! :
228+ b_tree_set ! : insert() to add elements
298229
299- 使用push添加元素
230+ hash_map! : insert() to add key-value pairs
300231
301- use push to add elements
232+ b_tree_map! : insert() to add key-value pairs
0 commit comments