1 个不稳定版本
0.1.0 | 2022年7月7日 |
---|
#44 in #条件
12KB
134 行
条件工具库
cond_utils
是一个非常简单的库,它提供了两个具有比较工具的特质:Between
和 In
。
cond_utils
的目标是简化并使一些常见的任务(如比较一个值是否在两个限制之间,或检查一个值是否在集合中)更加易于阅读。这使得我们可以编写如下代码
use cond_utils::Between;
let number = 6;
if number.between(0, 10) {
println!("Number is between 0 and 10");
}
而不是
let number = 6;
if number > 0 && number < 10 {
println!("Number is between 0 and 10");
}
这
use cond_utils::In;
let number = 6;
if number.is_in(&[2, 6, 12]) {
println!("Number is in set");
}
而不是
let number = 6;
if number == 2 || number == 6 || number == 12 {
println!("Number is in set");
}
或这
use cond_utils::In;
let number = 6;
if number.in_ranges(&[0..5, 10..100]) {
println!("Number is between 0 and 5 or between 10 and 100");
}
而不是
let number = 6;
if (number >= 0 && number <= 5) || (number >= 10 && number <= 100) {
println!("Number is between 0 and 5 or between 10 and 100");
}
它适用于任何实现了 PartialEq
+ PartialOrd
+ Sized
特质的类型。