10个稳定版本
2.2.3 | 2023年5月31日 |
---|---|
2.0.0 | 2023年3月11日 |
1.3.1 | 2023年3月1日 |
1.3.0 | 2022年10月15日 |
在调试类别中排名#291
每月下载量:99
用于netcomm
23KB
275 行
Info Utils
Rust中用于日志记录和错误处理的实用程序。
功能
-
log!
、warn!
和error!
宏,用于方便且信息丰富的数据显示 -
eval()
作为unwrap()
的替代品 -
should()
作为expect()
的替代品 -
Info Utils还提供了
eval_or()
、eval_or_default()
和eval_or_else()
函数,作为相应unwrap_*()
函数的替代品。
使用方法
将以下内容添加到你的cargo.toml
[dependencies]
info_utils = "2"
或者运行
cargo add info_utils
在Rust代码中使用
宏
基本使用
use info_utils::macros::*;
fn main() {
let x = 10;
log!("Value is {}", x);
warn!("{} is greater than 3", x);
error!("x is equal to {}, exiting..", x); // Exits with error code
}
输出
❯ cargo run
Compiling demo v0.1.0 (/Path/To/Project)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/demo`
info ["main"]: Value is 10
warn ["main"]: 10 is greater than 3
error ["main"]: x is equal to 10, exiting..
终端输出是彩色格式化的
使用命名线程
use info_utils::macros::*;
use std::thread;
fn main() {
log!("Hello from main thread!");
let named_thread = thread::Builder::new()
.name("named thread".into())
.spawn(||{
log!("Hello from inside a named thread!");
}).unwrap();
let unnamed_thread = thread::Builder::new()
.spawn(|| {
log!("Hello from inside an unnamed thread!");
}).unwrap();
named_thread.join().unwrap();
unnamed_thread.join().unwrap();
}
输出
❯ cargo run
Compiling demo v0.1.0 (/Path/To/Project)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/demo`
info ["main"]: Hello from main thread!
info ["named thread"]: Hello from inside a named thread!
info ["<unknown>"]: Hello from inside an unnamed thread!
评估
基本使用
use info_utils::prelude::*;
fn main() {
let option: Option<&str> = Some("valid value");
let mut result: Result<&str, &str> = Ok("everything's fine");
log!("Option: {}\nValue: {}", option.eval(), result.eval());
result = Err("oh no something happened!");
log!("Result: {}", result.eval());
}
输出
❯ cargo run
Compiling demo v0.1.0 (/Path/To/Project)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/demo`
info ["main"]: Option: valid value - Value: everything's fine
error ["main"]: "oh no something happened!"
其他函数
use info_utils::prelude::*;
fn main() {
let mut option: Option<&str> = Some("valid value");
let mut result: Result<&str, &str> = Ok("everything's fine");
log!("Option: {}\nValue: {}", option.eval(), result.eval());
result = Err("oh no something happened!");
option = None;
log!("Result: {}", result.eval_or("it's alright we can handle this"));
warn!("Option: {}", option.eval_or("option was None"));
log!("Result: {}", result.eval_or_default()); // Logs "" since that's the str default value
log!("Result: {}", result.eval_or_else(|e| {
warn!("error was: {:?}, but we're all good anyways", e);
"error handled"
}));
}
输出
❯ cargo run
Compiling demo v0.1.0 (/Path/To/Project)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/demo`
info ["main"]: Option: valid value - Value: everything's fine
info ["main"]: Result: it's alright we can handle this
warn ["main"]: Option: option was None
info ["main"]: Result:
warn ["main"]: error was: "oh no something happened!", but we're all good anyways
info ["main"]: Result: error handled
贡献
如果你在包或文档中注意到任何问题或错误,请创建一个问题或PR来解决它。
你也可以自由地提交功能请求的问题。