rayon为Rust实现的多线程并发库。
pub enum Cow
where
B: 'a + ToOwned + ?Sized,
{
Borrowed(&'a B),
Owned(::Owned),
}
A clone-on-write smart pointer. The type Cow
is a smart pointer providing clone-on-write functionality: it can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required. The type is designed to work with general borrowed data via the Borrow
trait. Cow implements Deref
, which means that you can call non-mutating methods directly on the data it encloses. If mutation is desired, to_mut
will obtain a mutable reference to an owned value, cloning if necessary. 例子有:
use std::borrow::Cow;
fn abs_all(input: &mut Cow) {
for i in 0..input.len() {
let v = input[i];
if v < 0 {
// Clones into a vector if not already owned.
input.to_mut()[i] = -v;
}
}
}
// No clone occurs because `input` doesn't need to be mutated.
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
abs_all(&mut input);
// Clone occurs because `input` needs to be mutated.
let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]);
abs_all(&mut input);
// No clone occurs because `input` is already owned.
let mut input = Cow::from(vec![-1, 0, 1]);
abs_all(&mut input);
3. Derivative
This crate provides a set of alternative #[derive]
attributes for Rust. derivative
uses attributes to make it possible to derive more implementations than the built-in derive(Trait)
. Here are a few examples of stuffs you cannot just derive
. 例子:
gmp_mpfr_sys
为Rust底层FFI(外部函数接口Foreign Function Interface)库,提供与GNU任意精度库的连接:
- FFI接口 for GMP:整数和有理数;
- FFI接口 for MPFR:浮点数
- FFI接口 for MPC:复数。
如果想要更上层的封装库,可考虑Rug
库。
cargo-watch
自动化构建工具通过运行cargo watch
命令,可监测所在代码工程里的变化,并运行相应的命令。监控修改并自动运行相应的命令,不用手工编译运行。
cargo install cargo-watch
# Run tests only
$ cargo watch -x test
# Run check then tests
$ cargo watch -x check -x test
# Run run with arguments
$ cargo watch -x 'run -- --some-arg'
# Run an arbitrary command
$ cargo watch -s 'echo Hello world'
6. cargo edit
cargo-edit
通过运行cargo add/rm/upgrade
等命令来对Cargo.toml
中的依赖项进行管理。 cargo-edit
内部使用rustfmt
来进行格式调整;利用clippy
来进行检查。
cargo install cargo-edit
$ # Add a specific version
$ cargo add regex@0.1.41 --dev
$ # Query the latest version from crates.io and adds it as build dependency
$ cargo add gcc --build
$ # Add a non-crates.io crate
$ cargo add local_experiment --path=lib/trial-and-error/
$ # Add a non-crates.io crate; the crate name will be found automatically
$ cargo add lib/trial-and-error/
$ # Add a crates.io crate with a local development path
$ cargo add my_helper --vers=1.3.1 --path=lib/my-helper/
$ # Add a renamed dependency
$ cargo add thiserror --rename error
7. borsh
Borsh: Borsh stands for Binary Object Representation Serializer for Hashing. It is meant to be used in security-critical projects as it prioritizes consistency, safety, speed, and comes with a strict specification. 举例:
use borsh::{BorshSerialize, BorshDeserialize};
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct A {
x: u64,
y: String,
}
#[test]
fn test_simple_struct() {
let a = A {
x: 3301,
y: "liber primus".to_string(),
};
let encoded_a = a.try_to_vec().unwrap();
let decoded_a = A::try_from_slice(&encoded_a).unwrap();
assert_eq!(a, decoded_a);
}
参考资料: [1] https://lib.rs/crates/rayon [2] https://rust.cc/article?id=4d539751-ff12-4abf-acc1-eaa39ae32c32 [3] https://doc.rust-lang.org/1.26.1/std/borrow/enum.Cow.html [4] https://mcarton.github.io/rust-derivative/ [5] https://docs.rs/gmp-mpfr-sys/1.1.14/gmp_mpfr_sys/ [6] https://github.com/passcod/cargo-watch [7] https://github.com/killercup/cargo-edit