#macro #operator #ternary #development-tools

macrors

Rust 中的一个简单自定义宏库

3 个不稳定版本

0.2.0 2024 年 7 月 3 日
0.1.1 2024 年 3 月 9 日
0.1.0 2024 年 3 月 9 日

#183过程宏

每月 27 次下载

Apache-2.0

15KB
153

macrors

一个简单的自定义 macro 库,在 Rust 中。

API 文档

1.使用方法

将此添加到您的 Cargo.toml

[dependencies]
macrors = "0.1"

2.APIs

2.1.运算符

2.1.1.ternary

let seed = 10;

let pos = ternary!(seed % 2 == 0, 1, -1);
let neg = ternary!((seed - 1) % 2 == 0, 1, -1);
assert_eq!(1, pos);
assert_eq!(-1, neg);
// bool
let seed = 10;

let is_even = ternary!(seed % 2 == 0, true, false);
assert!(is_even);
// &str
let seed = 10;

let result = ternary!(seed > 5, "greater than 5", "less than or equal to 5");
assert_eq!("greater than 5", result);

2.1.2.ternary_eq

let eq = ternary_eq!(2, 2, 1, -1);
let ne = ternary_eq!(1, 2, 1, -1);

assert_eq!(1, eq);
assert_eq!(-1, ne);
// date-time
let now = SystemTime::now();
let now_add = now.add(Duration::from_millis(1_000));
let now_sub = now.sub(Duration::from_millis(1_000));

let eq = ternary_eq!(now, now, 1, -1);
let ne = ternary_eq!(now, now.add(Duration::from_millis(1_000)), now_add, now_sub);

assert_eq!(1, eq);
assert_eq!(now_sub, ne);

2.1.3.ternary_ne

let ne = ternary_ne!(3, 2, 1, -1);
let eq = ternary_ne!(2, 2, 1, -1);

assert_eq!(1, ne);
assert_eq!(-1, eq);
// date-time
let now = SystemTime::now();
let now_add = now.add(Duration::from_millis(1_000));
let now_sub = now.sub(Duration::from_millis(1_000));

let ne = ternary_ne!(now, now.add(Duration::from_millis(1_000)), 1, -1);
let eq = ternary_ne!(now, now, now_add,now_sub);

assert_eq!(1, ne);
assert_eq!(now_sub, eq);

2.2.重复

  • @0.2.0

2.2.1.repeat!

let repeat_1 = repeat!("A", 5);
assert_eq!(vec!["A", "A", "A", "A", "A"], repeat_1);

let repeat_2 = repeat!(101, 5);
assert_eq!(vec![101, 101, 101, 101, 101], repeat_2);

2.2.2.repeat_str!

// Item: Alphabet
// Default separator -> `""`
let repeat_str_1 = repeat_str!("A", 5);
assert_eq!("AAAAA", repeat_str_1);

// Item: Alphabet
// Custem separator -> `,`
let repeat_str_2 = repeat_str!("A", 5, ",");
assert_eq!("A,A,A,A,A", repeat_str_2);

// Item: Number 
let repeat_str_3 = repeat_str!(101, 5, ",");
assert_eq!("101,101,101,101,101", repeat_str_3);

无运行时依赖