寫代碼的時候,在List上加N多個操作符非常的繁瑣,幸好有itertools幫我們處理,方便的很,比如你要對一個list做map完后排序,排序完后分組,你要怎么做?
1、clap
clap在寫一些CLI工具時非常有用,在ripgrep?和Rust自己的Cargo都在使用,clap編譯后非常小,而且加載啟動非???。clap會幫你處理命令行參數(shù),可以自定義友好提示,支持函數(shù)式操作,非常方便。
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是一個功能豐富且性能非常好的通用序列化庫。
生產(chǎn)中不建議自己造輪子讀寫文件,你可以先定義你要的數(shù)據(jù)類型,然后使用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?協(xié)議的客戶端庫,幫大多數(shù)人做了一些希望HTTP客戶端為他們做的許多事情,發(fā)請求,超時處理,多種格式支持,異步同步請求,代理等等。
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是一個可以并行提供數(shù)據(jù)的庫,它知道如何合理的拆分?jǐn)?shù)據(jù)塊并合理的使用cpu。比如你想在一個列表上并行使用map時就可以使用它。這就有意思了,在寫一些CLI工具的時候很有用,并不是所有的編程語言都可以在命令行并行跑一些數(shù)據(jù)。
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標(biāo)準(zhǔn)庫的一部分,可以考慮切換到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));