#语法 #集合 #哈希表 # #最小值 #比较

sugar

Rust 语法糖集合

2 个不稳定版本

使用旧的 Rust 2015

0.2.0 2018 年 4 月 3 日
0.1.0 2018 年 1 月 8 日

17#cmp 中排名

Download history 6/week @ 2024-03-14 1/week @ 2024-03-21 26/week @ 2024-03-28 12/week @ 2024-04-04 3/week @ 2024-04-11 4/week @ 2024-04-18 6/week @ 2024-04-25 3/week @ 2024-05-16 4/week @ 2024-05-23 9/week @ 2024-05-30 4/week @ 2024-06-06 58/week @ 2024-06-20 25/week @ 2024-06-27

每月下载量 89

MIT 许可证

8KB
128

Sugar

语法糖,让你的 Rust 生活更甜美。

用法

use sugar::*;

概述

use sugar::*;

// vec of boxed value
let vb1 = vec_box![1, 2, 3];

// list comprehension
let vb2 = c![Box::new(i), for i in 1..4];

// hashmap construction
let hm1 = hashmap!{
    1 => 2,
    2 => 3,
    3 => 4,
};

// hashmap comprehension
let hm2 = c![i => i + 1, for i in 1..4];

let _ = max!(1, 2, 3);
let _ = min!(1, 2, 3);

if cmp!(1, < num < 3) {
    println!("hello world");
}

更多详情请参阅 sugar 的 文档


lib.rs:

语法糖,让你的 Rust 生活更甜美。

用法

use sugar::*;

集合构造

use sugar::*;

// vec construction
let v = vec![1, 2, 3];

// vec of boxed value
let vb = vec_box![1, 2, 3];

// hashmap construction
let hm = hashmap!{
    1 => 2,
    2 => 3,
    3 => 4,
};

// list comprehension
let vb2 = c![Box::new(i), for i in 1..4];
assert_eq!(vb, vb2);

// hashmap comprehension
let hm2 = c![i => i + 1, for i in 1..4];
assert_eq!(hm, hm2);
  • vec_box!vec_box 重新导出
  • hashmap! 和相关宏从 maplit 包中重新导出
  • c! 列表推导宏从 cute 包中重新导出

哈希表:克隆特定键的值。

use sugar::*;

let hm = hashmap! {
    1 => "1".to_owned(),
    2 => "2".to_owned(),
};

let s = {
    hm.get_clone(&1)
};

assert_eq!(s.unwrap(), "1".to_owned());

结果:忽略 Result 的 OK 值

use sugar::SResultExt;

fn do_work1() -> Result<i32, String> {
    Ok(1)
}

fn do_work2() -> Result<(), String> {
    // I don't care about the result's Ok value
    do_work1()
        .drop_value()
}

do_work2();

max! 和 min! 宏

use sugar::*;

assert_eq!(max!(3, 1, 2, 7, 0), 7);
assert_eq!(min!(3, 1, 2, 7, 0), 0);

链式比较

use sugar::*;

fn return_2() -> i32 {
    return 2;
}

assert_eq!(cmp!(1, < 2, < 3), true);
assert_eq!(cmp!(1, <= 2, <= 3), true);
assert_eq!(cmp!(1, <= 2, <= 3, != 4), true);

// return_2 will not be called
assert_eq!(cmp!(2, < 1, < 3, < return_2()), false);

// return_2 will be called once
assert_eq!(cmp!(1, < return_2(), <= 2), true);

依赖关系

~39KB