Skip to content

Releases: Natural-selection1/better-comprehension-in-rust

v3.0.0

11 Apr 04:13
Compare
Choose a tag to compare

新增:

  • 支持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);
        }
    }
];

v2.0

10 Apr 02:55
Compare
Choose a tag to compare

新增:
* 可以使用块在返回之前执行其他代码

示例

use better_comprehension::vector;
let vec_1 = vec!["123".to_string(), "456".to_string()];
let vec_2 = vec!["abc".to_string(), "def".to_string()];
let vec = vector![
    {
        let some = x.clone() + y;
        println!("{}", some);

        (x.clone(), y.clone())
    }
    for x in vec_1 if x.contains("1")
    for y in vec_2 if y.contains("d")
];

v1.3.0

09 Apr 03:18
Compare
Choose a tag to compare

修复:

  • 在容器推导式中, 不允许字段访问的情况

改进:

  • 在文档中添加 "真实的样例"
  • 使迭代器推导式中的警告更加具体