zl程序教程

您现在的位置是:首页 >  Javascript

当前栏目

Rust中应该尝试使用的12个杀手级库,我们先来说几个!

2023-02-19 12:24:26 时间

1、clap

clap在写一些CLI工具时非常有用,在ripgrep​和Rust自己的Cargo都在使用,clap编译后非常小,而且加载启动非常快。clap会帮你处理命令行参数,可以自定义友好提示,支持函数式操作,非常方便。

https://clap.rs

use clap::App;

fn main() {
App::new("myapp")
.version("1.0")
.about("Does great things!")
.author("Kevin K.")
.get_matches();
}

$ myapp --help
myapp 1.0
Kevin K.
Does great things!

USAGE:
myapp [FLAGS]

FLAGS:
-h, --help Prints this message
-V, --version Prints version information

2、serde

和calp一样,serde是一个功能丰富且性能非常好的通用序列化库。

生产中不建议自己造轮子读写文件,你可以先定义你要的数据类型,然后使用serde库加载读写文件。另外serde库可以帮你完成YAML,JSON的序列化和反序化,非常的方便。

https://serde.rs

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}

fn main() {
let point = Point { x: 1, y: 2 };

// Convert the Point to a JSON string.
let serialized = serde_json::to_string(&point).unwrap();

// Prints serialized = {"x":1,"y":2}
println!("serialized = {}", serialized);

// Convert the JSON string back to a Point.
let deserialized: Point = serde_json::from_str(&serialized).unwrap();

// Prints deserialized = Point { x: 1, y: 2 }
println!("deserialized = {:?}", deserialized);
}

3、reqwest

reqwest和request​一样,是一个http​协议的客户端库,帮大多数人做了一些希望HTTP客户端为他们做的许多事情,发请求,超时处理,多种格式支持,异步同步请求,代理等等。

https://docs.rs/reqwest/0.10.8/reqwest

// This will POST a body of `{"lang":"rust","body":"json"}`
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");

let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
.json(&map)
.send()
.await?;

4、rayon

rayon是一个可以并行提供数据的库,它知道如何合理的拆分数据块并合理的使用cpu。比如你想在一个列表上并行使用map时就可以使用它。这就有意思了,在写一些CLI工具的时候很有用,并不是所有的编程语言都可以在命令行并行跑一些数据。

https://github.com/rayon-rs/rayon

use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
input.par_iter() // <-- just change that!
.map(|&i| i * i)
.sum()
}

5、slog and log

slog​是Rust中非常完整的一个日志库。很多有名的库也使用它来做日志库,比如 term,json​等等。但是log​库正在成为Rust标准库的一部分,可以考虑切换到log库上。

https://github.com/slog-rs/slog

let decorator = slog_term::PlainSyncDecorator::new(std::io::stdout());
let drain = slog_term::FullFormat::new(decorator).build().fuse();

let root_log = slog::Logger::root(drain, o!("version" => "0.5"));
let server_log = root_log.new(o!("host" => "localhost", "port" => "8080"));
let peer1_log =
server_log.new(o!("peer_addr" => "8.8.8.8", "port" => "18230"));
let peer2_log =
server_log.new(o!("peer_addr" => "82.9.9.9", "port" => "42381"));

info!(server_log, "starting");
info!(server_log, "listening");
debug!(peer2_log, "connected");
debug!(peer2_log, "message received"; "length" => 2);
debug!(peer1_log, "connected");
debug!(peer2_log, "response sent"; "length" => 8);
debug!(peer2_log, "disconnected");
debug!(peer1_log, "message received"; "length" => 2);
debug!(peer1_log, "response sent"; "length" => 8);
debug!(peer1_log, "disconnected");
info!(server_log, "exit");

6、itertools

写代码的时候,在List上加N多个操作符非常的繁琐,幸好有itertools帮我们处理,方便的很,比如你要对一个list做map完后排序,排序完后分组,你要怎么做?

https://github.com/rust-itertools/itertools

 // using Itertools::fold_results to create the result of parsing
let irises = DATA.lines()
.map(str::parse)
.fold_ok(Vec::new(), |mut v, iris: Iris| {
v.push(iris);
v
});
let mut irises = match irises {
Err(e) => {
println!("Error parsing: {:?}", e);
std::process::exit(1);
}
Ok(data) => data,
};

// Sort them and group them
irises.sort_by(|a, b| Ord::cmp(&a.name, &b.name));