11# better_comprehension
22
3- 在rust中的集合推导式和迭代器推导式。提供更好的Rust使用体验。
3+ 在rust中的集合推导式和迭代器推导式。提供更好的Rust使用体验
4+
45Collection comprehension and Iterator comprehension in Rust. And it provides a better experience in Rust.
56
67# Usage
78语法源自[ python推导式] ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ) 。
8- 本库为Rust标准库中的所有集合类型提供宏,以及基于引用的迭代器。
9+ 本库为Rust标准库中的所有集合类型提供宏,以及基于引用的迭代器
10+
911The syntax is derived from [ Python's comprehension] ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions ) .
12+
1013This library provides macros for all collection types in the Rust standard library and an Iterator based on references.
1114
15+ ---
1216简单示例
1317simple example
1418``` rust
@@ -17,7 +21,9 @@ let vec: Vec<String> = vector![x.clone() for x in vec_1];
1721assert_eq! (vec , vec! [" AB" . to_string (), " CD" . to_string ()]);
1822```
1923
24+ ---
2025你也可以在推导式中使用模式
26+
2127You can also use patterns in it
2228``` rust
2329struct Person {
@@ -29,15 +35,17 @@ let people = [Person { name: "Joe".to_string(), age: 20 },
2935let vec_deque = vec_deque! [name . clone () for Person { name , .. } in people ];
3036assert_eq! (vec_deque , VecDeque :: from ([" Joe" . to_string (), " Bob" . to_string ()]));
3137```
32-
38+ ---
3339过滤值
40+
3441filtering values
3542``` rust
3643let linked_list = linked_list! [ i * 2 for i in 1 ..= 3 if i != 2 ];
3744assert_eq! (linked_list , LinkedList :: from ([2 , 6 ]));
3845```
39-
46+ ---
4047根据条件返回不同的值
48+
4149return different values based on conditions
4250``` rust
4351let b_tree_set = b_tree_set! {
@@ -46,8 +54,9 @@ let b_tree_set = b_tree_set!{
4654 };
4755assert_eq! (b_tree_set , BTreeSet :: from ([1 , 13 ]));
4856```
49-
57+ ---
5058嵌套推导式
59+
5160nested comprehension
5261``` rust
5362let binary_heap = binary_heap! [
@@ -56,8 +65,9 @@ let binary_heap = binary_heap![
5665 for j in 1 ..= 3 if j + i != 4 ];
5766assert_eq! (binary_heap . into_sorted_vec (), vec! [1 , 1 , 3 , 13 ]);
5867```
59-
68+ ---
6069和python的推导式一样, 本库的for循环是从上到下读取的.
70+
6171the reading order of the for loop in this library is from top to bottom, just like Python's comprehension.
6272``` rust
6373let vec = vector! [
@@ -68,7 +78,9 @@ assert_eq!(vec, vec![(1, 4), (1, 5), (1, 6), (3, 4), (3, 5), (3, 6)]);
6878```
6979
7080需要注意的是, 由于在rust中, for loop 是消耗所有权的.
81+
7182所以通常来说, 对于多层循环, 如果你希望原容器被消耗, 你应该写成如下这样:
83+
7284Note that in Rust, for loops consume ownership.
7385So typically, for nested loops, if you want the original container to be consumed, you should write it like this:
7486
@@ -114,8 +126,11 @@ println!("{:?}", vec_2); // work well
114126```
115127
116128但在本库中, 你不需要这么做, 提供的宏会自动帮你处理这些问题.
117- 你唯一需要做的就是在你想要保留所有权的变量后面加上.iter() 或 使用 & , 其余会在宏内自动处理.
129+
118130But in this library, you don't need to do this, the provided macros will automatically handle these problems for you.
131+
132+ 你唯一需要做的就是在你想要保留所有权的变量后面加上.iter() 或 使用 & , 其余会在宏内自动处理.
133+
119134You only need to add .iter() or use & before the variable you want to keep ownership, the rest will be automatically handled in the macro.
120135``` rust
121136let vec_1 = vec! [" ABC" . to_string (), " DEF" . to_string ()];
@@ -155,9 +170,15 @@ let vec_key = vec!["key_1".to_string(), "key_2".to_string(), "key_3".to_string()
155170 );
156171```
157172
158- 该库也支持迭代器推导式, 但不同于上面的集合推导式, 该迭代器推导式是基于引用的, 所以不会消耗所有权.
159- 除此之外的写法与集合推导式完全相同.
160- Iterator comprehension is also supported, but unlike the collection comprehension above, this iterator comprehension is based on references, so it will not consume ownership.
173+ 该库也支持迭代器推导式, 但不同于上面的集合推导式
174+
175+ 该迭代器推导式是基于引用的, 所以不会消耗所有权
176+
177+ 除此之外的写法与集合推导式完全相同
178+
179+ Iterator comprehension is also supported,but unlike the collection comprehension above,
180+
181+ this iterator comprehension is based on references,so it will not consume ownership.
161182``` rust
162183let vec_1 = [" 123" . to_string (), " 456" . to_string (), " 789" . to_string ()];
163184let vec_2 = [" ABC" . to_string (), " DEF" . to_string (), " GHI" . to_string ()];
186207```
187208
188209以上写法与下面的写法是完全的等价形式
210+
189211The above writing is equivalent to the following writing
190212``` rust
191213let vec_1 = [" 123" . to_string (), " 456" . to_string (), " 789" . to_string ()];
@@ -231,33 +253,49 @@ let mut result3 = {
231253# some details
232254
233255vector! :
234- 使用push添加元素
235- use push to add elements
256+
257+ 使用push添加元素
258+
259+ use push to add elements
236260
237261vec_deque! :
238- 使用push_back添加元素
239- use push_back to add elements
262+
263+ 使用push_back添加元素
264+
265+ use push_back to add elements
240266
241267linked_list! :
242- 使用push_back添加元素
243- use push_back to add elements
268+
269+ 使用push_back添加元素
270+
271+ use push_back to add elements
244272
245273hash_set! :
246- 使用insert添加元素
247- use insert to add elements
274+
275+ 使用insert添加元素
276+
277+ use insert to add elements
248278
249279hash_map! :
250- 使用insert添加键值对
251- use insert to add key-value pairs
280+
281+ 使用insert添加键值对
282+
283+ use insert to add key-value pairs
252284
253285b_tree_map! :
254- 使用insert添加键值对
255- use insert to add key-value pairs
286+
287+ 使用insert添加键值对
288+
289+ use insert to add key-value pairs
256290
257291b_tree_set! :
258- 使用insert添加元素
259- use insert to add elements
292+
293+ 使用insert添加元素
294+
295+ use insert to add elements
260296
261297binary_heap! :
262- 使用push添加元素
263- use push to add elements
298+
299+ 使用push添加元素
300+
301+ use push to add elements
0 commit comments