Skip to content

Commit 4d364c9

Browse files
在文档中更新市场调研说明
1 parent 64e4254 commit 4d364c9

File tree

4 files changed

+160
-16
lines changed

4 files changed

+160
-16
lines changed

README-CN.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

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

5+
本库旨在成为所有推导式库的良好替代品, 对于你在crate.io中搜索"comprehension"时遇到的库, 我们已经做到了 :
6+
* [comprehension](https://crates.io/crates/comprehension)
7+
* 暂未支持let变量绑定
8+
* 其余功能本库完全覆盖
9+
* [kt-list-comprehensions](https://crates.io/crates/kt-list-comprehensions)
10+
* 所有功能本库完全覆盖
11+
* [list_comprehension_macro](https://crates.io/crates/list_comprehension_macro)
12+
* 暂未提供一个统一的宏, 通过mapping表达式进行区分(就像真正的python推导式那样)
13+
* 暂未支持while loop
14+
* 其余功能本库完全覆盖
15+
* [iter-comprehensions](https://crates.io/crates/iter-comprehensions)
16+
* 所有功能本库完全覆盖
17+
* [list_comprehension](https://crates.io/crates/list_comprehension)
18+
* 暂未支持let else变量绑定
19+
* 其余功能本库完全覆盖
20+
* [cute](https://crates.io/crates/cute)
21+
* 所有功能本库完全覆盖
22+
523
# 用法
624
语法源自 [python推导式](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)
725

@@ -45,6 +63,24 @@ assert_eq!(linked_list, LinkedList::from([2, 6]));
4563
```
4664
---
4765

66+
使用块在返回前执行代码
67+
```rust
68+
use better_comprehension::vector;
69+
let vec_1 = vec!["123".to_string(), "456".to_string()];
70+
let vec_2 = vec!["abc".to_string(), "def".to_string()];
71+
let vec = vector![
72+
{
73+
let some = x.clone() + y;
74+
println!("{}", some);
75+
76+
(x.clone(), y.clone())
77+
}
78+
for x in vec_1 if x.contains("1")
79+
for y in vec_2 if y.contains("d")
80+
];
81+
```
82+
---
83+
4884
根据条件返回不同的值
4985
```rust
5086
use better_comprehension::b_tree_set;

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@
44
Collection comprehension and Iterator comprehension in Rust.
55
And it provides a better experience in Rust.
66

7+
This library aims to be a good alternative to all comprehension libraries,
8+
for the libraries you encounter when searching "comprehension" on crate.io,
9+
we have already done:
10+
* [comprehension](https://crates.io/crates/comprehension)
11+
* Not supported let variable binding
12+
* All other features are covered
13+
* [kt-list-comprehensions](https://crates.io/crates/kt-list-comprehensions)
14+
* All features are covered
15+
* [list_comprehension_macro](https://crates.io/crates/list_comprehension_macro)
16+
* Not provided a unified macro, using mapping expression to distinguish
17+
(like the real python comprehension)
18+
* Not supported while loop
19+
* All other features are covered
20+
* [iter-comprehensions](https://crates.io/crates/iter-comprehensions)
21+
* All features are covered
22+
* [list_comprehension](https://crates.io/crates/list_comprehension)
23+
* Not supported let else variable binding
24+
* All other features are covered
25+
* [cute](https://crates.io/crates/cute)
26+
* All features are covered
27+
728
# Usage
829

930
The syntax is derived from [Python's comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions).
@@ -45,6 +66,25 @@ let linked_list = linked_list![ i*2 for i in 1..=3 if i != 2 ];
4566
assert_eq!(linked_list, LinkedList::from([2, 6]));
4667
```
4768
---
69+
70+
use block to execute code before returning
71+
```rust
72+
use better_comprehension::vector;
73+
let vec_1 = vec!["123".to_string(), "456".to_string()];
74+
let vec_2 = vec!["abc".to_string(), "def".to_string()];
75+
let vec = vector![
76+
{
77+
let some = x.clone() + y;
78+
println!("{}", some);
79+
80+
(x.clone(), y.clone())
81+
}
82+
for x in vec_1 if x.contains("1")
83+
for y in vec_2 if y.contains("d")
84+
];
85+
```
86+
---
87+
4888
return different values based on conditions
4989
```rust
5090
use better_comprehension::b_tree_set;

src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
//! Collection comprehension and Iterator comprehension in Rust.
55
//! And it provides a better experience in Rust.
66
//!
7+
//! This library aims to be a good alternative to all comprehension libraries,
8+
//! for the libraries you encounter when searching "comprehension" on crate.io,
9+
//! we have already done:
10+
//! * [comprehension](https://crates.io/crates/comprehension)
11+
//! * Not supported let variable binding
12+
//! * All other features are covered
13+
//! * [kt-list-comprehensions](https://crates.io/crates/kt-list-comprehensions)
14+
//! * All features are covered
15+
//! * [list_comprehension_macro](https://crates.io/crates/list_comprehension_macro)
16+
//! * Not provided a unified macro, using mapping expression to distinguish
17+
//!
18+
//! (like the real python comprehension)
19+
//! * Not supported while loop
20+
//! * All other features are covered
21+
//! * [iter-comprehensions](https://crates.io/crates/iter-comprehensions)
22+
//! * All features are covered
23+
//! * [list_comprehension](https://crates.io/crates/list_comprehension)
24+
//! * Not supported let else variable binding
25+
//! * All other features are covered
26+
//! * [cute](https://crates.io/crates/cute)
27+
//! * All features are covered
28+
//!
29+
//!
730
//! # Usage
831
//!
932
//! The syntax is derived from [Python's comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions).
@@ -12,6 +35,7 @@
1235
//! in the Rust standard library and an Iterator based on references.
1336
//!
1437
//! ---
38+
//!
1539
//! simple example
1640
//! ```rust
1741
//! use better_comprehension::vector;
@@ -20,6 +44,7 @@
2044
//! assert_eq!(vec, vec!["AB".to_string(), "CD".to_string()]);
2145
//! ```
2246
//! ---
47+
//!
2348
//! You can also use patterns in it
2449
//! ```rust
2550
//! use better_comprehension::vec_deque;
@@ -37,6 +62,7 @@
3762
//! "Bob".to_string()]));
3863
//! ```
3964
//! ---
65+
//!
4066
//! filtering values before comprehension
4167
//! ```rust
4268
//! use better_comprehension::linked_list;
@@ -45,6 +71,25 @@
4571
//! assert_eq!(linked_list, LinkedList::from([2, 6]));
4672
//! ```
4773
//! ---
74+
//!
75+
//! use block to execute code before returning
76+
//!```rust
77+
//!use better_comprehension::vector;
78+
//!let vec_1 = vec!["123".to_string(), "456".to_string()];
79+
//!let vec_2 = vec!["abc".to_string(), "def".to_string()];
80+
//!let vec = vector![
81+
//! {
82+
//! let some = x.clone() + y;
83+
//! println!("{}", some);
84+
//!
85+
//! (x.clone(), y.clone())
86+
//! }
87+
//! for x in vec_1 if x.contains("1")
88+
//! for y in vec_2 if y.contains("d")
89+
//!];
90+
//!```
91+
//! ---
92+
//!
4893
//! return different values based on conditions
4994
//! ```rust
5095
//! use better_comprehension::b_tree_set;
@@ -56,6 +101,7 @@
56101
//! assert_eq!(b_tree_set, BTreeSet::from([1, 13]));
57102
//! ```
58103
//! ---
104+
//!
59105
//! nested comprehension
60106
//! ```rust
61107
//! use better_comprehension::binary_heap;
@@ -67,6 +113,7 @@
67113
//! assert_eq!(binary_heap.into_sorted_vec(), vec![1, 1, 3, 13]);
68114
//! ```
69115
//! ---
116+
//!
70117
//! the reading order of the for loop in this library is from top to bottom,
71118
//! just like Python's comprehension.
72119
//! ```rust
@@ -123,6 +170,7 @@
123170
//! // println!("{:?}", vec_3); // borrow of moved value
124171
//! ```
125172
//! ---
173+
//!
126174
//! But in this library, you don't need to do this,
127175
//!
128176
//! the provided macros will automatically handle these problems for you.
@@ -148,6 +196,7 @@
148196
//! // println!("{:?}", vec_3); // borrow of moved value
149197
//! ```
150198
//! ---
199+
//!
151200
//! This library also supports key-value collection types, HashMap, BTreeMap
152201
//! ```rust
153202
//! use better_comprehension::hash_map;
@@ -174,6 +223,7 @@
174223
//! );
175224
//! ```
176225
//! ---
226+
//!
177227
//! Iterator comprehension is also supported,
178228
//! but unlike the collection comprehension above,
179229
//!

src/main.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,52 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDequ
77

88
fn main() {
99
test_vec();
10-
test_binary_heap();
11-
test_linked_list();
12-
test_b_tree_set();
13-
test_b_tree_map();
14-
test_vec_deque();
15-
test_hash_set();
16-
test_hash_map();
17-
test_custom_type();
18-
test_ref_iterator();
19-
test_pattern_matching();
20-
test_nested_comprehension();
21-
test_ownership_handling();
22-
test_option();
23-
some_real_example_2();
10+
// test_binary_heap();
11+
// test_linked_list();
12+
// test_b_tree_set();
13+
// test_b_tree_map();
14+
// test_vec_deque();
15+
// test_hash_set();
16+
// test_hash_map();
17+
// test_custom_type();
18+
// test_ref_iterator();
19+
// test_pattern_matching();
20+
// test_nested_comprehension();
21+
// test_ownership_handling();
22+
// test_option();
23+
// some_real_example_2();
2424
}
2525

2626
fn test_vec() {
27+
{
28+
let vec_1 = vec!["123".to_string(), "456".to_string()];
29+
let vec_2 = vec!["abc".to_string(), "def".to_string()];
30+
let vec = vector![
31+
{
32+
let some = x.clone() + y;
33+
println!("{}", some);
34+
35+
(x.clone(), y.clone())
36+
}
37+
for x in vec_1 if x.contains("1")
38+
for y in vec_2 if y.contains("d")
39+
];
40+
}
41+
2742
// 简单示例
2843
{
2944
let vec_1 = vec!["AB".to_string(), "CD".to_string()];
30-
let vec: Vec<String> = vector![x.clone() for x in vec_1];
45+
let vec: Vec<String> = vector![{println!("{}", x);
46+
x.clone()}
47+
for x in vec_1];
3148
assert_eq!(vec, vec!["AB".to_string(), "CD".to_string()]);
3249
}
3350

3451
// 条件返回不同值
3552
{
3653
let result = vector![
37-
[x, y] if x > y else [y, x]
54+
{println!("{}", x); [x, y]} if x > y
55+
else {println!("{}", y); [y, x]}
3856
for y in 0..7 if y % 3 == 0
3957
for x in (0..y+2) if x % 2 == 0
4058
];

0 commit comments

Comments
 (0)