better_comprehension
在rust中的集合推导式和迭代器推导式。提供更好的Rust使用体验
Collection comprehension and Iterator comprehension in Rust. And it provides a better experience in Rust.
Usage
语法源自python推导式。 本库为Rust标准库中的所有集合类型提供宏,以及基于引用的迭代器
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
let vec_1 = vec!;
let vec: = vector!;
assert_eq!;
你也可以在推导式中使用模式
You can also use patterns in it
let people = ;
let vec_deque = vec_deque!;
assert_eq!;
过滤值
filtering values
let linked_list = linked_list!;
assert_eq!;
根据条件返回不同的值
return different values based on conditions
let b_tree_set = b_tree_set!;
assert_eq!;
嵌套推导式
nested comprehension
let binary_heap = binary_heap!;
assert_eq!;
和python的推导式一样, 本库的for循环是从上到下读取的.
the reading order of the for loop in this library is from top to bottom, just like Python's comprehension.
let vec = vector!;
assert_eq!;
需要注意的是, 由于在rust中, for loop 是消耗所有权的.
所以通常来说, 对于多层循环, 如果你希望原容器被消耗, 你应该写成如下这样:
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:
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.
你唯一需要做的就是在你想要保留所有权的变量后面加上.iter() 或 使用 & , 其余会在宏内自动处理.
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.
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
同时, 该库还支持键值对容器类型, HashMap, BTreeMap This library also supports key-value collection types, HashMap, BTreeMap
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.
let vec_1 = ;
let vec_2 = ;
let mut result3 = iterator_ref!;
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添加元素
use push to add elements
vec_deque! :
使用push_back添加元素
use push_back to add elements
linked_list! :
使用push_back添加元素
use push_back to add elements
hash_set! :
使用insert添加元素
use insert to add elements
hash_map! :
使用insert添加键值对
use insert to add key-value pairs
b_tree_map! :
使用insert添加键值对
use insert to add key-value pairs
b_tree_set! :
使用insert添加元素
use insert to add elements
binary_heap! :
使用push添加元素
use push to add elements