自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Rust中應(yīng)該嘗試使用的12個殺手級庫,我們先來說幾個!

開發(fā) 前端
寫代碼的時候,在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));
責(zé)任編輯:武曉燕 來源: 碼小菜
相關(guān)推薦

2021-09-29 08:59:49

Rust編程語言

2022-07-06 08:39:33

Python代碼

2024-02-29 18:05:21

Code插件工具

2019-03-27 08:32:26

邊緣計算網(wǎng)絡(luò)

2014-11-05 09:34:06

開源監(jiān)測工具

2022-10-20 15:16:23

JavaScript數(shù)組技能

2023-12-06 18:06:37

Git開發(fā)

2024-08-13 00:23:48

2023-12-21 18:01:58

Docker容器部署

2023-12-03 18:26:25

IDEA插件

2023-04-10 14:49:35

Web應(yīng)用程序工具

2012-04-20 09:52:02

移動應(yīng)用

2013-07-09 09:55:16

Windows 8.1

2024-02-19 00:00:00

Project開發(fā)項目

2023-11-24 18:10:38

開發(fā)Visual

2023-10-07 18:03:18

Code插件WSL

2023-11-01 18:01:02

改進(jìn)WakaTime編程

2018-06-15 22:41:06

開源軟件React軟件開發(fā)

2023-11-09 18:07:25

Pycharm插件

2011-06-27 15:18:15

SEO
點贊
收藏

51CTO技術(shù)棧公眾號