|
4 | 4 | //! Collection comprehension and Iterator comprehension in Rust. |
5 | 5 | //! And it provides a better experience in Rust. |
6 | 6 | //! |
| 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 | +//! |
7 | 30 | //! # Usage |
8 | 31 | //! |
9 | 32 | //! The syntax is derived from [Python's comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions). |
|
12 | 35 | //! in the Rust standard library and an Iterator based on references. |
13 | 36 | //! |
14 | 37 | //! --- |
| 38 | +//! |
15 | 39 | //! simple example |
16 | 40 | //! ```rust |
17 | 41 | //! use better_comprehension::vector; |
|
20 | 44 | //! assert_eq!(vec, vec!["AB".to_string(), "CD".to_string()]); |
21 | 45 | //! ``` |
22 | 46 | //! --- |
| 47 | +//! |
23 | 48 | //! You can also use patterns in it |
24 | 49 | //! ```rust |
25 | 50 | //! use better_comprehension::vec_deque; |
|
37 | 62 | //! "Bob".to_string()])); |
38 | 63 | //! ``` |
39 | 64 | //! --- |
| 65 | +//! |
40 | 66 | //! filtering values before comprehension |
41 | 67 | //! ```rust |
42 | 68 | //! use better_comprehension::linked_list; |
|
45 | 71 | //! assert_eq!(linked_list, LinkedList::from([2, 6])); |
46 | 72 | //! ``` |
47 | 73 | //! --- |
| 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 | +//! |
48 | 93 | //! return different values based on conditions |
49 | 94 | //! ```rust |
50 | 95 | //! use better_comprehension::b_tree_set; |
|
56 | 101 | //! assert_eq!(b_tree_set, BTreeSet::from([1, 13])); |
57 | 102 | //! ``` |
58 | 103 | //! --- |
| 104 | +//! |
59 | 105 | //! nested comprehension |
60 | 106 | //! ```rust |
61 | 107 | //! use better_comprehension::binary_heap; |
|
67 | 113 | //! assert_eq!(binary_heap.into_sorted_vec(), vec![1, 1, 3, 13]); |
68 | 114 | //! ``` |
69 | 115 | //! --- |
| 116 | +//! |
70 | 117 | //! the reading order of the for loop in this library is from top to bottom, |
71 | 118 | //! just like Python's comprehension. |
72 | 119 | //! ```rust |
|
123 | 170 | //! // println!("{:?}", vec_3); // borrow of moved value |
124 | 171 | //! ``` |
125 | 172 | //! --- |
| 173 | +//! |
126 | 174 | //! But in this library, you don't need to do this, |
127 | 175 | //! |
128 | 176 | //! the provided macros will automatically handle these problems for you. |
|
148 | 196 | //! // println!("{:?}", vec_3); // borrow of moved value |
149 | 197 | //! ``` |
150 | 198 | //! --- |
| 199 | +//! |
151 | 200 | //! This library also supports key-value collection types, HashMap, BTreeMap |
152 | 201 | //! ```rust |
153 | 202 | //! use better_comprehension::hash_map; |
|
174 | 223 | //! ); |
175 | 224 | //! ``` |
176 | 225 | //! --- |
| 226 | +//! |
177 | 227 | //! Iterator comprehension is also supported, |
178 | 228 | //! but unlike the collection comprehension above, |
179 | 229 | //! |
|
0 commit comments