better_comprehension
Collection comprehension and Iterator comprehension in Rust. And it provides a better experience in Rust.
This library aims to be a good alternative to all comprehension libraries, for the libraries you encounter when searching "comprehension" on crate.io, we have already done:
- comprehension
- Not supported let variable binding
- (In the 2.0.0 version, it partially supports let variable binding, which can be used in the block return statement)
- All other features are covered
- Not supported let variable binding
- kt-list-comprehensions
- All features are covered
- list_comprehension_macro
- Not provided a unified macro, using mapping expression to distinguish (like the real python comprehension)
- Not supported while loop
- All other features are covered
- iter-comprehensions
- All features are covered
- list_comprehension
- Not supported let else variable binding
- All other features are covered
- cute
- All features are covered
Usage
The syntax is derived from Python's comprehension.
This library provides macros for all collection types in the Rust standard library and an Iterator based on references.
simple example
use vector;
let vec_1 = vec!;
let vec: = vector!;
assert_eq!;
You can also use patterns in it
use vec_deque;
use VecDeque;
let people = ;
let vec_deque = vec_deque!;
assert_eq!;
filtering values before comprehension
use linked_list;
use LinkedList;
let linked_list = linked_list!;
assert_eq!;
use block to execute code before returning
use vector;
let vec_1 = vec!;
let vec_2 = vec!;
let vec = vector!;
return different values based on conditions
use b_tree_set;
use BTreeSet;
let b_tree_set = b_tree_set!;
assert_eq!;
nested comprehension
use binary_heap;
use BinaryHeap;
let binary_heap = binary_heap!;
assert_eq!;
the reading order of the for loop in this library is from top to bottom, just like Python's comprehension.
use vector;
let vec = vector!;
assert_eq!;
Note that in Rust, for loops consume ownership. So typically, for nested loops, if you want the original container to be consumed, you should write it like this:
use vector;
let vec_1 = vec!;
let vec_2 = vec!;
let vec_3 = vec!;
let vec = ;
// println!("{:?}", vec_1); // borrow of moved value
println!; // work well
// println!("{:?}", vec_3); // borrow of moved value
But in this library, you don't need to do this,
the provided macros will automatically handle these problems for you.
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.
use vector;
let vec_1 = vec!;
let vec_2 = vec!;
let vec_3 = vec!;
let vec = vector!;
// println!("{:?}", vec_1); // borrow of moved value
println!; // work well
// println!("{:?}", vec_3); // borrow of moved value
This library also supports key-value collection types, HashMap, BTreeMap
use hash_map;
use HashMap;
let vec_key = vec!;
let vec_value = ;
let hash_map = hash_map!;
assert_eq!;
Iterator comprehension is also supported, but unlike the collection comprehension above,
this iterator comprehension is based on references, so it will not consume ownership. By the way, to ensure the correctness of the iterator comprehension, only two types of iterable objects are allowed to be passed in:
- single identifier (not followed by any method calls)
- range expression (e.g., 1..=3 or 1..x where x is an number)
use iterator_ref;
let vec_1 = ;
let vec_2 = ;
let mut result3 = iterator_ref!;
// still alive
println!;
println!;
for _ in 0..=9
/*
Some(("123", "ABC"))
Some(("123", "DEF"))
Some(("123", "GHI"))
Some(("123", "ABC"))
Some(("123", "DEF"))
Some(("123", "GHI"))
Some(("789", "DEF"))
Some(("789", "DEF"))
None
None
*/
The above writing is equivalent to the following writing
let vec_1 = ;
let vec_2 = ;
let mut result3 = ;
some details
vector! : push() to add elements
vec_deque! : push_back() to add elements
linked_list! : push_back() to add elements
binary_heap! : push() to add elements
hash_set! : insert() to add elements
b_tree_set! : insert() to add elements
hash_map! : insert() to add key-value pairs
b_tree_map! : insert() to add key-value pairs
some real examples
use vector;
use ;
// create a 3x3 matrix
let matrix = vector!;
// transpose the matrix
let transposed = vector!;
// matrix is alive
assert_eq!;
assert_eq!;
use ;
use ;
let students_data = ;
// use for loop
let math_scores: = ;
// ↓ is equivalent to ↓
// use comprehension!
let math_scores: = hash_map!;
assert_eq!;
// use for loop
let high_scores = ;
// ↓ is equivalent to ↓
// use comprehension!
let high_scores = b_tree_map!;
assert_eq!;