文章目录
一、Cargo
官网: https://doc.rust-lang.org/cargo/index.html
cargo,简单来说就是python 的pip,nodejs 的npm,rust下的包管理工具。
Cargo用于组织Rust项目,比直接用rustc编译多个源文件更方便。
Rust 由 rustup 工具来安装和管理。 Rust 有一个 6 周的 快速发布过程 并且支持 大量的平台 ,所以任何时候都有很多 Rust 构建可用。 rustup 在 Rust 支持的每一个平台上以一致的方式管理这些构建, 并可以从 beta 和 nightly 发布渠道安装 Rust,且支持额外的交叉编译目标平台。
cargo new project_name --bin # 如果你想写一个普通的项目
cargo new lib_name --lib --vcs none # 如果你想写一个库
cargo build # 如果你想编译,默认会编译到target/debug/project_name下
cargo run # 如果你想编译并运行
cargo build --release # 如果你想发布,这会做很多优化,并编译到target/release/project_name下
cargo update # 如果你想修改Cargo.lock文件的话,运行它
cargo update -p rand # 如果你只是想更新rand版本的话,运行它
cargo test abc # 如果你想做test,运行它
1. 配置cargo国内源
原文链接:https://blog.csdn.net/u010953692/article/details/106464851
rust cargo指定国内镜像
参考URL: https://blog.csdn.net/setlilei/article/details/106204105?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
/root/.cargo新建配置文件config
[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
cargo install cargo-rls-install
cargo rls-install -i nightly-2020-03-19
rustc -V
2. cargo来进行项目构建
-
执行
cargo new hello_cargo --bin
,执行完上面的操作之后,我们切换到hell_cargo目录下,可以看到一个文件(Cargo.toml)和一个目录(src),同时src目录下有一个main.rs文件。 -
执行 cargo run 就可以看到一行"hello world" 字符串出现在屏幕上
3. cargo run运行项目
可以使用 cargo run 在一个命令中同时编译并运行生成的可执行文件。当然,也可以使用 cargo run --release 运行发布版本。
demo:
cargo run -p aptos-node --release -- -f /opt/aptos/public_full_node.yaml
当我们通过cargo run运行程序时,会调用target目录下面的可执行程序。
-p: 指定运行target中哪个包
–release: 优化编译,编译时间会变长
–: 双虚线将参数与cargo分开,明确指定参数-f 是传递给应用程序。
因此如果需要将参数传递给应用程序,而不是cargo,需要使用两个虚线将cargo run与参数分开。
[root@dev release]# cargo run -h
cargo-run
Run a binary or example of the local package
USAGE:
cargo run [OPTIONS] [--] [args]...
ARGS:
<args>...
OPTIONS:
-q, --quiet Do not print cargo log messages
--bin [<NAME>] Name of the bin target to run
--example [<NAME>] Name of the example target to run
-p, --package [<SPEC>...] Package with the target to run
-v, --verbose Use verbose output (-vv very verbose/build.rs output)
-j, --jobs <N> Number of parallel jobs, defaults to # of CPUs
--color <WHEN> Coloring: auto, always, never
-r, --release Build artifacts in release mode, with optimizations
--frozen Require Cargo.lock and cache are up to date
--profile <PROFILE-NAME> Build artifacts with the specified profile
--features <FEATURES> Space or comma separated list of features to activate
--locked Require Cargo.lock is up to date
--all-features Activate all available features
--offline Run without accessing the network
--config <KEY=VALUE> Override a configuration value (unstable)
--no-default-features Do not activate the `default` feature
--target <TRIPLE> Build for the target triple
-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for
details
--target-dir <DIRECTORY> Directory for all generated artifacts
--manifest-path <PATH> Path to Cargo.toml
--message-format <FMT> Error format
--unit-graph Output build graph in JSON (unstable)
--ignore-rust-version Ignore `rust-version` specification in packages
--timings[=<FMTS>...] Timing output formats (unstable) (comma separated): html, json
-h, --help Print help information
Run `cargo help run` for more detailed information.
[root@dev release]# cargo run --release -h
cargo-run
Run a binary or example of the local package
USAGE:
cargo run [OPTIONS] [--] [args]...
ARGS:
<args>...
OPTIONS:
-q, --quiet Do not print cargo log messages
--bin [<NAME>] Name of the bin target to run
--example [<NAME>] Name of the example target to run
-p, --package [<SPEC>...] Package with the target to run
-v, --verbose Use verbose output (-vv very verbose/build.rs output)
-j, --jobs <N> Number of parallel jobs, defaults to # of CPUs
--color <WHEN> Coloring: auto, always, never
-r, --release Build artifacts in release mode, with optimizations
--frozen Require Cargo.lock and cache are up to date
--profile <PROFILE-NAME> Build artifacts with the specified profile
--features <FEATURES> Space or comma separated list of features to activate
--locked Require Cargo.lock is up to date
--all-features Activate all available features
--offline Run without accessing the network
--config <KEY=VALUE> Override a configuration value (unstable)
--no-default-features Do not activate the `default` feature
--target <TRIPLE> Build for the target triple
-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for
details
--target-dir <DIRECTORY> Directory for all generated artifacts
--manifest-path <PATH> Path to Cargo.toml
--message-format <FMT> Error format
--unit-graph Output build graph in JSON (unstable)
--ignore-rust-version Ignore `rust-version` specification in packages
--timings[=<FMTS>...] Timing output formats (unstable) (comma separated): html, json
-h, --help Print help information
Run `cargo help run` for more detailed information.
[root@dev release]#
二、Cargo.toml配置项参数说明
https://doc.rust-lang.org/cargo/reference/index.html#cargo-reference
1. 工作常用配置参数
[[bin]] 二进制目标
二进制目标是可执行程序,可以在编译后运行。 默认二进制文件名是src / main.rs,默认为包的名称。 其他二进制文件存储在SRC / BIN /目录中。 每个二进制文件的设置可以在Cargo.Toml中的[[Bin]]表中定制。
您可以使用带有 --bin <bin-name>
的cargo run
命令运行单个二进制文件。 cargo install
安装可用于将可执行文件复制到common 位置。
# Example of customizing binaries in Cargo.toml.
[[bin]]
name = "cool-tool"
test = false
bench = false
[[bin]]
name = "frobnicator"
required-features = ["frobnicate"]
Cargo.toml中的[workspace]members起什么作用?
members相当于你自己可以在src中添加其它的二进制package,然后可以引用这些二进制package里的东西;
dependencies则是针对于library package
Cargo工作空间
这是为了开发大型程序,分治crate用的。
一,根cargo.toml内容
[workspace]
members = [
“adder”,
“add-one”,
]
二,adder里的cargo.toml内容
[package]
name = “adder”
version = “0.1.0”
authors = [“test test@qq.com”]
edition = “2018”
[dependencies]
add-one = { path = “…/add-one” }
三,main.rs内容
use add_one;
fn main() {
let num = 10;
println!(“Hello, world! {} plus one is {}!”, num, add_one::add_one(num));
}
四,Lib.rs内容
pub fn add_one(x: i32) -> i32 {
x + 1
}
#[cfg(test)]
mod tests {
use super:😗;
#[test]
fn it_works() {
assert_eq!(3, add_one(2));
}
}
[lib]
Rust 中的 bin, lib, rlib, a, so 概念介绍
【Rust每周一知】Rust 中的 bin, lib, rlib, a, so 概念介绍
参考URL: https://blog.csdn.net/u012067469/article/details/104013445
我们创建一个新工程,通常从下面两句入手:
cargo new foobar
或
cargo new --lib foobar
前者创建一个可执行工程,而后者创建一个库工程。
你去探索上述命令行生成的文件,发现它们的 Cargo.toml 完全一样,区别仅在于 src 目录下,可执行工程是一个 main.rs,而库工程是一个 lib.rs。
这是因为 main.rs 和 lib.rs 对于一个 crate 来讲,是两个特殊的文件名。rustc 内置了对这两个特殊文件名的处理(当然也可以通过 Cargo.toml 进行配置),我们可以认为它们就是一个 crate 的入口。
crate type
rustc --help|grep crate-type
可得到如下输出
--crate-type [bin|lib|rlib|dylib|cdylib|staticlib|proc-macro]
-
bin
二进制可执行 crate,编译出的文件为二进制可执行文件。必须要有 main 函数作为入口。这种 crate 不需要在 Cargo.toml 中或 --crate-type 命令行参数中指定,会自动识别。 -
lib
库 crate。它其实并不是一种具体的库,它指代后面各种库 crate 中的一种,可以认为是一个代理名称(alias)。
通常来讲,如果什么都不配置,默认指的是 rlib, 会生成 .rlib 的文件。 -
rlib
rlib 是 Rust Library 特定静态中间库格式。如果只是纯 Rust 代码项目之间的依赖和调用,那么,用 rlib 就能完全满足使用需求。
rlib 中包含很多 metadata 信息(比如可能的上游依赖信息),用来做后面的 linkage。
在 Cargo.toml 中配置:
[lib]
name = "foobar"
crate-type = ["rlib"]
可以指定生成 rlib,但是一般没必要设置,因为默认 lib 就是 rlib。
rlib 是平台(Linux, MacOS, Windows …)无关的。
- dylib
动态库。
在 Cargo.toml 中配置:
[lib]
name = "foobar"
crate-type = ["dylib"]
会在编译的时候,生成动态库(Linux 上为 .so, MacOS 上为 .dylib, Windows 上为 .dll)。
动态库是平台相关的库。动态库在被依赖并链接时,不会被链接到目标文件中。这种动态库只能被 Rust 写的程序(或遵循 Rust 内部不稳定的规范的程序)调用。这个动态库可能依赖于其它动态库(比如,Linux 下用 C 语言写的 PostgreSQL 的 libpq.so,或者另一个编译成 “dylib” 的 Rust 动态库)。
- cdylib
C规范动态库。
在 Cargo.toml 中配置:
[lib]
name = "foobar"
crate-type = ["cdylib"]
**与 dylib 类似,也会生成 .so, .dylib 或 .dll 文件。但是这种动态库可以被其它语言调用(因为几乎所有语言都有遵循 C 规范的 FFI 实现),也就是跨语言 FFI 使用。**这个动态库可能依赖于其它动态库(比如,Linux 下用 C 语言写的 PostgreSQL 的 libpq.so)。
- staticlib
静态库。
在 Cargo.toml 中配置:
[lib]
name = "foobar"
crate-type = ["staticlib"]
编译会生成 .a 文件(在 Linux 和 MacOS 上),或 .lib 文件(在 Windows 上)。
编译器会把所有实现的 Rust 库代码以及依赖的库代码全部编译到一个静态库文件中,也就是对外界不产生任何依赖了。这特别适合将 Rust 实现的功能封装好给第三方应用使用。