sudo dnf install rust-toolset
rustc -Vcargo -V
sudo dnf install rustuprustup toolchain install stable
cargo -V 验证安装结果,此时能看到最新发布的稳定版本的版本号、提交哈希值和提交日期。rustup toolchain install stable 安装:rustup toolchain install stableinfo: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'stable-x86_64-unknown-linux-gnu unchanged - rustc 1.69.0 (84c898d65 2023-04-16)error: rustup is not installed at '/root/.cargo'
cargo new 指令,可以一键构建 Rust 项目,譬如新建一个经典项目:cargo new hello_world --bin # 早期cargo必须设置--bin,以指定创建bin类型项目cargo new hello_world # 作用同上
cargo new --lib hello_lib
bin 和 lib,前者是一个可运行的项目,后者是一个依赖库项目。cargo 在创建项目时,必须添加 --bin 参数。当前提供版本的 cargo 默认创建 bin 类型的项目,故无需显式指定 --bin 参数。tree.├── Cargo.toml└── src└── main.rs
Cargo.toml: Cargo 特有的项目描述文件或 manifest 元清单,它包含了 Cargo 编译项目所需的所有元数据。Cargo.lock:Cargo.lock 文件是 Cargo 工具根据同一项目的 toml 文件生成的项目依赖详细清单,一般无需修改。src 目录:源代码存放位置。src/main.rs:主可执行文件,编辑项目代码时修改 main.rs 并将新的源文件添加到 src 子目录。benches 目录:基准测试 benchmark 存放目录。examples 目录:示例代码存放目录。tests 目录:集成测试代码存放目录。Cargo.toml 文件中格式解读如下:[package]: 定义项目( package )的元信息name: 项目名称version: 项目版本authors: 项目作者edition: 当前使用的Rust大版本rust-version: Rust最低要求description: 项目描述documentation: 文档readme: README文件homepage: 主页repository: 源代码仓库license: 开源协议License.license-file: License文件keywords: 项目关键词categories: 项目分类workspace: 工作空间 workspacebuild: 脚本编译路径links: 本地链接库的名称exclude: 发布时排除文件include: 发布时包含文件publish: 用于阻止项目的发布metadata: 额外的配置信息,用于提供给外部工具default-run: [cargo run] 所使用的默认可执行文件( binary )autobins: 禁止可执行文件的自动发现autoexamples: 禁止示例文件的自动发现autotests: 禁止测试文件的自动发现autobenches: 禁止 bench 文件的自动发现resolver: 设置依赖解析器( dependency resolver)
cargo build
# 查看文件类型file target/debug/hello_worldtarget/debug/hello_world: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=c87cf5694fab5ba052821001f3d9c31070543413, for GNU/Linux 3.2.0, with debug_info, not stripped# 手动运行./target/debug/hello_world
cargo runCompiling hello_world v0.1.0 (/root/test/hello_world)Finished dev [unoptimized + debuginfo] target(s) in 0.25sRunning `target/debug/hello_world`
cargo run 首先对项目进行编译,然后再运行,因此它实际上等同于运行了两个分步指令,对于使用者而言可以简化操作。debug 模式。tree -L 2.├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target├── CACHEDIR.TAG└── debug # debug模式
debug 模式下,代码编译速度快,开发者可以流畅编译。但同时意味着运行速度会变慢,在发布时会带来负面影响。所以您也可以在发布时,显示指定为 release 模式:cargo run --releasecargo build --release
release 目录:tree -L 2.├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target├── CACHEDIR.TAG├── debug└── release # release模式
cargo run 和 cargo build 会较为耗时。此时,可以通过 cargo check 对代码进行快速编译:cargo checkFinished dev [unoptimized + debuginfo] target(s) in 0.02s
() 或 Result<(), E> where E: Error 。cargo test # debug 模式cargo test --release # release 模式
/// 对其进行标记;cargo doc --no-deps
rustfmt 是一个用于格式化 Rust 代码的工具,可以帮助开发者确保代码的整洁、一致性和可读性。使用 rustfmt 可以将 Rust 代码自动格式化为符合社区推荐的代码风格,提供一致的代码规范。 rustfmt 既可以独立使用,也可以与 Cargo 一起使用。rustfmt 之前,请确保安装了 rustfmt 。如果尚未安装,可以通过运行以下命令轻松地安装它:dnf install rustfmt
rustfmt 后,您可以按照以下方式使用它:rustfmt 命令后,程序将根据默认配置规范格式化指定的 Rust 源文件。默认情况下, rustfmt 会将格式化过的内容直接输出到终端。rustfmt src/test.rs
rustfmt 直接修改源文件,以原地覆盖的方式格式化,可以使用 --emit 标志:rustfmt --emit files src/test.rs
rustfmt xx.rs 可以格式化指定文件,而使用 cargo fmt 命令则可以自动应用 rustfmt 格式化规则,为项目中所有的 .rs 文件进行格式化。cargo fmt
rustfmt.toml 的文件。在这个文件中,可以添加省略引号的键值对配置,例如:max_width = 100reorder_imports = true
rustfmt.toml 文件后, rustfmt 会自动应用其中的配置选项进行格式化。Cargo.toml 后,无法通过校验。error: the listed checksum of `/builddir/build/BUILD/gnome-tour-43.0/vendor/glib-macros/Cargo.toml` has changed:expected: d6e10d3ec19f19b5a32cdcd159567ede9fa1e59ac13f446f0a1e40f99e7e6c1bactual: ea00aa6587f4f1793c296fd4a18c9989239e13df703c9254906b1c757714d440directory sources are not intended to be edited, if modifications are required then it is recommended that `[patch]` is used with a forked copy of the source
.cargo-checksum.json 导致,在该 json 文件中找到 Cargo.toml 内容并修改为实际值即可。其余文件的校验错误同理。# 将"Cargo.toml":"d6e10d3ec19f19b5a32cdcd159567ede9fa1e59ac13f446f0a1e40f99e7e6c1b"# 修改为 "Cargo.toml":"ea00aa6587f4f1793c296fd4a18c9989239e13df703c9254906b1c757714d440"
Cargo.toml 中描述对于版本的要求,Cargo.lock 为根据编译时环境自动生成。Cargo.toml 文件,可知编译要求 gtk3 版本为 0.4.x。[dependencies]gtk = { package = "gtk3", version = "0.4", features= ["v4_2"]}log = "0.4"gettext-rs = { version = "0.7", features = ["gettext-system"] }adw = {package = "libadwaita", version = "0.1"}pretty_env_logger = "0.4"regex = "1.5"
Cargo.lock 时,编译源中提供 gtk3 版本为 0.4.8,故 Cargo.lock 中会锁定版本为 0.4.8。
当后续使用时,源中提供 gtk3 升级为 0.4.9,此时编译会报错:error: failed to select a version for the requirement `gtk3 = "^0.4"` (locked to 0.4.8)candidate versions found which didn't match: 0.4.9
gnome-tour-43.0 中的 Cargo.toml 可知,gtk3-0.4.9 已满足编译要求,因此该报错并非必要,需更新 Cargo.lock 文件以消除此报错。由于 Cargo.lock 由 Cargo.toml 自动生成,通常不建议手动修改,而应通过调整 Cargo.toml 来触发更新。gnome-tour-43.0 的 vendor 目录下包含 gtk3 文件夹,请将 vendor/gtk3/Cargo.toml 中的 version 更新为 0.4.9。[package]edition = "2021"rust-version = "1.56"name = "gtk3"version = "0.4.9"
gnome-tour-43.0 目录下执行 cargo update,即可自动更新 Cargo.lock 文件:cargo updateUpdating gtk3 v0.4.8 -> v0.4.9
文档反馈