Skip to content

v3.0.0

Latest

Choose a tag to compare

@Natural-selection1 Natural-selection1 released this 11 Apr 04:13
· 11 commits to master since this release

新增:

  • 支持let表达式

详见: https://github.com/Natural-selection1/better-comprehension-in-rust/blob/e8f59c30588a94229528fe2ac15c71a3e5361aaf/README-CN.md?plain=1#L117-L163

let 表达式

let 表达式所属的范围是它上方最近的 for in 表达式,且受到 if 表达式筛选后的结果 (如果有的话), 可以有多个 let 表达式,他们的阅读顺序是从上到下的.
完全等价于

for pattern in collection {
    if ... { // 如果有的话
    let ...;
    let ...;

    }
}

使用 let 表达式绑定变量

use better_comprehension::vector;
let vec = vector![
    b
    for x in 1..=3 if x != 2
    let __x__ = x*2
    for y in 4..=6 if y+__x__ != 7
    let z = __x__ + y
    let a = z*2
    let b = match z {
        5..=6 => 1,
        7..=8 => 2,
        _ => 3
    }
];
assert_eq!(vec, vec![1, 2, 3, 3, 3]);

使用 let _ = 或 let () = 执行任意代码

这是一个极其强大的功能,请谨慎使用

use better_comprehension::vector;
let vec = vector![
    x
    for x in 1..=3
    let _ = println!("{}", x)
    let () = {
        for i in 1..=3 {
            println!("{}", i);
        }
    }
];