Skip to content

Commit 27bc4c0

Browse files
更新文档与README
1 parent 61d1855 commit 27bc4c0

File tree

5 files changed

+145
-80
lines changed

5 files changed

+145
-80
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ keywords = [
1212
]
1313
repository = "https://github.com/Natural-selection1/better-comprehension-in-rust"
1414
readme = "README.md"
15-
version = "1.0.2"
15+
version = "1.1.0"
1616
edition = "2024"
1717
exclude = ["src/main.rs", "target", ".vscode", "README-CN.md",".gitignore"]
1818

README-CN.md

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# better_comprehension
22

3-
在rust中的集合推导式和迭代器推导式提供更好的Rust使用体验
3+
在rust中的集合推导式和迭代器推导式, 提供更好的Rust使用体验
44

55
# 用法
6-
语法源自[python推导式](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)
6+
语法源自 [python推导式]
7+
(https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)
78

89
本库为Rust标准库中的所有集合类型提供宏,以及基于引用的迭代器
910

@@ -65,7 +66,8 @@ let vec = vector![
6566
(top,bottom)
6667
for top in 1..=3 if top != 2
6768
for bottom in 4..=6 if bottom+top != 4];
68-
assert_eq!(vec, vec![(1, 4), (1, 5), (1, 6), (3, 4), (3, 5), (3, 6)]);
69+
assert_eq!(vec, vec![(1, 4), (1, 5), (1, 6),
70+
(3, 4), (3, 5), (3, 6)]);
6971
```
7072

7173
需要注意的是, 由于在rust中, for loop 是消耗所有权的.
@@ -107,7 +109,8 @@ println!("{:?}", vec_2); // work well
107109

108110
但在本库中, 你不需要这么做, 提供的宏会自动帮你处理这些问题.
109111

110-
你唯一需要做的就是在你想要保留所有权的变量后面加上.iter() 或 使用 & , 其余会在宏内自动处理.
112+
你唯一需要做的就是在你想要保留所有权的变量后面`加上.iter()``使用 &`,
113+
其余会在宏内自动处理.
111114
```rust
112115
let vec_1 = vec!["ABC".to_string(), "DEF".to_string()];
113116
let vec_2 = vec!["abc".to_string(), "def".to_string()];
@@ -128,23 +131,25 @@ println!("{:?}", vec_2); // work well
128131
同时, 该库还支持键值对容器类型, HashMap, BTreeMap
129132

130133
```rust
131-
let vec_key = vec!["key_1".to_string(), "key_2".to_string(), "key_3".to_string()];
132-
let vec_value = [1, 2, 3];
133-
let hash_map = hash_map!{
134-
key.clone() : *value // 三种键值对分隔符都支持
135-
// key.clone() => *value
136-
// key.clone() , *value
137-
for key in vec_key
138-
for value in vec_value
139-
};
140-
assert_eq!(
141-
hash_map,
142-
HashMap::from([
143-
("key_1".to_string(), 3),
144-
("key_2".to_string(), 3),
145-
("key_3".to_string(), 3)
146-
])
147-
);
134+
let vec_key = vec!["key_1".to_string(),
135+
"key_2".to_string(),
136+
"key_3".to_string()];
137+
let vec_value = [1, 2, 3];
138+
let hash_map = hash_map!{
139+
key.clone() : *value // 三种键值对分隔符都支持
140+
// key.clone() => *value
141+
// key.clone() , *value
142+
for key in vec_key
143+
for value in vec_value
144+
};
145+
assert_eq!(
146+
hash_map,
147+
HashMap::from([
148+
("key_1".to_string(), 3),
149+
("key_2".to_string(), 3),
150+
("key_3".to_string(), 3)
151+
])
152+
);
148153
```
149154
---
150155

@@ -154,15 +159,22 @@ let vec_key = vec!["key_1".to_string(), "key_2".to_string(), "key_3".to_string()
154159

155160
除此之外的写法与集合推导式完全相同
156161
```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()];
162+
let vec_1 = ["123".to_string(),
163+
"456".to_string(),
164+
"789".to_string()];
165+
let vec_2 = ["ABC".to_string(),
166+
"DEF".to_string(),
167+
"GHI".to_string()];
159168

160169
let mut result3 = iterator_ref![
161170
(x.clone(), y.clone()) if x.contains("1") else (y.clone(), x.clone())
162171
for x in vec_1 if x.contains("1") || x.contains("7")
163172
for i in 1..=2
164173
for y in vec_2 if y.contains("D") || x.contains("3")];
165174

175+
// still alive
176+
println!("{:?}", vec_1);
177+
println!("{:?}", vec_2);
166178

167179
for _ in 0..=9 {
168180
println!("{:?}", result3.next());
@@ -183,8 +195,13 @@ None
183195

184196
以上写法与下面的写法是完全的等价形式
185197
```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()];
198+
let vec_1 = ["123".to_string(),
199+
"456".to_string(),
200+
"789".to_string()];
201+
let vec_2 = ["ABC".to_string(),
202+
"DEF".to_string(),
203+
"GHI".to_string()];
204+
188205
let mut result3 = {
189206
let vec_2 = vec_2.iter().collect::<Vec<_>>();
190207
let vec_1 = vec_1.iter().collect::<Vec<_>>();
@@ -215,18 +232,18 @@ let mut result3 = {
215232

216233
# 一些细节
217234

218-
vector! : 使用push()添加元素
235+
vector! : push() 添加元素
219236

220-
vec_deque! : 使用push_back()添加元素
237+
binary_heap! : push() 添加元素
221238

222-
linked_list! : 使用push_back()添加元素
239+
vec_deque! : push_back() 添加元素
223240

224-
hash_set! : 使用insert()添加元素
241+
linked_list! : push_back() 添加元素
225242

226-
hash_map! : 使用insert()添加键值对
243+
hash_set! : insert() 添加元素
227244

228-
b_tree_map! : 使用insert()添加键值对
245+
hash_map! : insert() 添加键值对
229246

230-
b_tree_set! : 使用insert()添加元素
247+
b_tree_map! : insert() 添加键值对
231248

232-
binary_heap! : 使用push()添加元素
249+
b_tree_set! : insert() 添加元素

README.md

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
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
---
1417
simple example
@@ -55,18 +58,22 @@ let binary_heap = binary_heap![
5558
assert_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
6064
let 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

6873
Note 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
7279
let 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

110117
the 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

114122
the rest will be automatically handled in the macro.
115123
```rust
@@ -130,39 +138,52 @@ println!("{:?}", vec_2); // work well
130138
---
131139
This 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

160177
let mut result3 = iterator_ref![
161178
(x.clone(), y.clone()) if x.contains("1") else (y.clone(), x.clone())
162179
for x in vec_1 if x.contains("1") || x.contains("7")
163180
for i in 1..=2
164181
for 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+
166187
for _ in 0..=9 {
167188
println!("{:?}", result3.next());
168189
}
@@ -183,8 +204,13 @@ None
183204

184205
The 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+
188214
let mut result3 = {
189215
let vec_2 = vec_2.iter().collect::<Vec<_>>();
190216
let vec_1 = vec_1.iter().collect::<Vec<_>>();

0 commit comments

Comments
 (0)