rust基础库及实用小工具

1. rayon

rayon为Rust实现的多线程并发库。
在这里插入图片描述

2. std::borrow::Cow

pub enum Cow<'a, B> 
where
    B: 'a + ToOwned + ?Sized, 
 {
    Borrowed(&'a B),
    Owned(<B as ToOwned>::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<[i32]>) {
    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.
例子:
在这里插入图片描述

4. gmp_mpfr_sys

gmp_mpfr_sys为Rust底层FFI(外部函数接口Foreign Function Interface)库,提供与GNU任意精度库的连接:

  • FFI接口 for GMP:整数和有理数;
  • FFI接口 for MPFR:浮点数
  • FFI接口 for MPC:复数。

如果想要更上层的封装库,可考虑Rug库。

5. cargo watch

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 [email protected] --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

参考资料:
[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

发布了154 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/mutourend/article/details/102951813