你需要知道的 32 個 Rust 庫 - 2
7,Paste
Paste crate 允許在編譯時連接標識符,這在編寫宏以使用宏變量和靜態文字創建任意標識符時非常有用。
在下面的例子中,定義了一個宏,這個宏爲一個名爲 $name 的類型創建了一個 impl 塊,併爲每個 $ 字段創建了 getter 方法。
macro_rules! make_a_struct_and_getters {
($name:ident { $($field:ident),* }) => {
// ...
// Build an impl block with getters. This expands to:
// impl S {
// pub fn get_a(&self) -> &str { &self.a }
// pub fn get_b(&self) -> &str { &self.b }
// pub fn get_c(&self) -> &str { &self.c }
// }
paste! {
impl $name {
$(
pub fn [<get_ $field>](&self) -> &str {
&self.$field
}
)*
}
}
}
}
make_a_struct_and_getters!(S { a, b, c });
fn call_some_getters(s: &S) -> bool {
s.get_a() == s.get_b() && s.get_c().is_empty()
}
8,Either
Either 枚舉有兩個變體 Left 和 Right,它具有多種方法和特性,便於使用該枚舉進行工作。
use either::Either;
#[test]
fn test() {
let values = vec![
Either::Left(1),
Either::Right(true),
Either::Left(10),
Either::Right(false),
];
assert_eq!(
values
.into_iter()
.map(|int_or_bool| -> Either<i32, bool> {
let int = either::try_left!(int_or_bool);
Either::Left(int * 2)
})
.map(|int_or_bool| { either::for_both!(int_or_bool, s => s.to_string()) })
.collect::<Vec<_>>(),
["2", "true", "20", "false"]
);
}
9,Num
數值特徵和類型的集合。包括數字、大整數、複數等的泛型。
use anyhow::{anyhow, Result};
use num::*;
use std::fmt::Display;
fn bounds_to_string<N: Bounded + Display>(number: N) -> String {
format!(
"value {} min is {} max is {}",
number,
N::min_value(),
N::max_value()
)
}
#[test]
fn bounds() {
assert_eq!(bounds_to_string(12u8), "value 12 min is 0 max is 255");
assert_eq!(
bounds_to_string(33i16),
"value 33 min is -32768 max is 32767"
);
}
fn num_operations<N: Num>(a: &str, b: N) -> Result<N> {
let a = N::from_str_radix(a, 10).map_err(|_| anyhow!("could not conert value"))?;
let value = a + b - N::one();
Ok(if value.is_zero() {
value
} else {
value * (N::one() + N::one())
})
}
#[test]
fn test_num_operations() -> Result<()> {
assert_eq!(num_operations("2", 10i32)?, 22i32);
assert_eq!(num_operations("-5", 6i8)?, 0i8);
Ok(())
}
#[test]
fn greatest_common_divisor() -> Result<()> {
assert_eq!(num::integer::gcd(25u8, 15u8), 5);
assert_eq!(num::integer::gcd(1024i32, 65536i32), 1024);
Ok(())
}
10,Thiserror
Thiserror crate 提供了一個宏用於在結構體和枚舉上實現 std::error::Error 特性。
從錯誤處理的角度來看,有兩種類型的 crate:庫和應用程序。
庫是作爲第三方依賴項創建的,將在應用程序中使用。對於庫 crate,重要的是調用代碼可以檢查庫代碼中發生了什麼類型的錯誤,並針對不同類型的錯誤實現不同的行爲。
對於應用程序來說,錯誤的具體類型通常並不重要,因此應用程序函數通常返回 Result<T, anyway::Error> 類型,因爲 anyway 允許使用? 操作符或 From trait。
Thiserror crate 主要用於在庫 crate 中方便地實現錯誤。
使用的例子:
#[derive(thiserror::Error, Debug)]
pub enum SomeError {
#[error("io error")]
Io(#[from] std::io::Error),
#[error("int parsing error")]
ParseInt(#[from] std::num::ParseIntError),
#[error("unknown error")]
General(#[from] anyhow::Error),
}
/// library func
fn int_error(s: &str) -> Result<i32, SomeError> {
let num = i32::from_str_radix(s, 10)?;
Ok(num + 2)
}
#[test]
fn test() {
// application code
assert!(matches!(int_error("abc").unwrap_err(), SomeError::ParseInt(_)));
assert!(matches!(
std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into(),
SomeError::Io(_)
));
}
在上面的例子中,std::num::ParseIntError 錯誤被轉換爲 SomeError::ParseInt。如果沒有 Thiserror 這個庫,我們將不得不手動編寫所有這些轉換。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/99mPuIie6qWg0Y2qyaGALQ