7个版本 (破坏性)
0.6.0 | 2022年12月8日 |
---|---|
0.5.0 | 2022年2月7日 |
0.4.1 | 2021年5月26日 |
0.4.0 | 2020年10月1日 |
0.1.0 | 2020年9月23日 |
#545 in 数据结构
9,785 每月下载量
用于 3 个crate (2 个直接)
80KB
3K SLoC
可迭代
用于Rust类似集合类型的可迭代库。
安装
# Cargo.toml
[dependencies]
iterable = "0.6"
功能
迭代集合类型而无需使用 iter()
和 collect()
use iterable::*;
fn main() {
// convert a vec of i32 to a vec of String
let v = vec![1, 2, 3];
// only one `map` function instead of `v.iter().map(|i| i.to_string()).collect()`
let res = v.map(|i| i.to_string());
assert_eq!(res, vec!["1".to_string(), "2".to_string(), "3".to_string()]);
// iterable also support array and string
let v = [1, 2, 3];
// res's type: [i32; 3]
let res = v.rev();
assert_eq!(res, [3, 2, 1]);
// lazy combinator
let v = vec![1,2,3];
let s = v
.lazy_filter(|x| x > &1)
.lazy_map(|x| x.to_string())
.rev();
assert_eq!(s, vec!["3".to_string(), "2".to_string()]);
// iterate over reference
let v = vec![1, 2, 3];
let res = (&v).filter(|i| i > &&1);
assert_eq!(res, vec![&2, &3]);
// functional append
let a = vec![1, 2, 3];
let res = a.add_one(1);
assert_eq!(res, vec![1, 2, 3 ,1]);
// a bunch of try methods: try_map, try_add_one, try_flat_map, try_flatten
let a = vec![1, 2, 3];
let res = a.try_map(|x| Some(x));
assert_eq!(res, Some(vec![1, 2, 3]));
let a = vec![1, 2, 3];
let res: Result<_,()> = a.try_map(|x| Ok(x));
assert_eq!(res, Ok(vec![1, 2, 3]));
}
许可证
MIT
依赖
~425KB