7 个版本

使用旧的 Rust 2015

0.3.0 2017年7月17日
0.2.1 2017年7月17日
0.1.3 2017年7月16日

1291Rust 模式 中排名

Download history 102/week @ 2023-11-27 91/week @ 2023-12-04 110/week @ 2023-12-11 143/week @ 2023-12-18 67/week @ 2023-12-25 53/week @ 2024-01-01 123/week @ 2024-01-08 97/week @ 2024-01-15 97/week @ 2024-01-22 98/week @ 2024-01-29 99/week @ 2024-02-05 185/week @ 2024-02-12 153/week @ 2024-02-19 126/week @ 2024-02-26 193/week @ 2024-03-04 32/week @ 2024-03-11

516 每月下载次数
用于 8 个crate (6 直接)

MIT 许可证

21KB
225

Cute

Build Status Build status Crates.io License: MIT

Rust 中 Python 风格列表推导的宏。

c! 宏实现了类似于 Python 中的列表和哈希表推导,允许使用条件语句和嵌套推导。

Python 语法

squares = [x*x for x in range(10)]

even_squares = [x*x for x in range(10) if x % 2 == 0]

squares_dict = {key:key*key for key in range(10)}

c! 语法

let squares = c![x*x, for x in 0..10];

let even_squares = c![x*x, for x in 0..10, if x % 2 == 0];

let squares_hashmap = c!{key => key*key, for key in 0..10};

注意 c! 的组成部分以逗号分隔。

示例

简单推导

#[macro_use(c)]
extern crate cute;

let v = [1,2,3,4];
let v_squared = c![x*x, for x in v];

条件过滤

let squares = c![x*x, for x in 0..10, if x % 2 == 0];
assert_eq!(squares, vec![0, 4, 16, 36, 64]);

嵌套推导

let nested = vec![vec![1,2,3], vec![4,5,6], vec![7,8,9]];
let flat: Vec<usize> = c![x, for x in y, for y in nested];
assert_eq!(flat, vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
let nested = vec![vec![1,2,3], vec![4,5,6], vec![7,8,9]];
let even_flat: Vec<usize> = c![x, for x in y, for y in nested, if x % 2 == 0];
assert_eq!(even_flat, vec![2, 4, 6, 8]);

迭代器上的推导

let vec: Vec<i32> = vec![-4, -2, 0, 2, 4];
let output: Vec<i32> = c![x*2, for x in vec.iter()];
assert_eq!(output, vec![-8, -4, 0, 4, 8]);
let vec: Vec<i32> = vec![-4, -2, 0, 2, 4];
let output: Vec<i32> = c![x, for x in vec.iter(), if *x >= 0i32];
assert_eq!(output, vec![0, 2, 4]);

函数应用

let vec: Vec<i32> = vec![-4, -2, 0, 2, 4];
let output: Vec<i32> = c![x.abs(), for x in vec.iter()];
assert_eq!(output, vec![4, 2, 0, 2, 4]);
fn square(x:i32) -> i32 {
       x*x
}
      
let vec: Vec<i32> = vec![-4, -2, 0, 2, 4];
let squares: Vec<i32> = c![square(x), for x in vec];
assert_eq!(squares, vec![16, 4, 0, 4, 16]);

哈希表推导(字典推导)

// simple hashmap comprehension

let squares_hashmap = c!{key => key*key, for key in 0..10};

// hashmap comprehension from an Iterator
// NOTE: we have to perform dereferencing: *key

let map = c!{*key => key*key, for key in vec![1,2].iter()};

// conditional hashmap comprehension

let v: Vec<(&str, i32)> = vec![("one", 1), ("two", 2), ("three", 3)];
let map = c! {key => val, for (key, val) in v, if val == 1 || val == 2};

let mut expected: HashMap<&str, i32> = HashMap::new();
expected.insert("one", 1);
expected.insert("two", 2);

assert_eq!(map, expected);
// conditional hashmap comprehension from an Iterator
// NOTE: we perform deferencing when using values

let map = c! {*key => key*key, for key in vec![1,2].iter(), if *key % 2 == 0};
let mut e: HashMap<i32, i32> = HashMap::new();
e.insert(2, 4);

assert_eq!(map, e);

无运行时依赖