TencentOS Server Rust 开发指南

Rust 是一款高效、可靠的通用高级语言,主打“安全、并发、实用”。Cargo 是 Rust 的构建系统和包管理器。

相关产品
TencentOS Server 产品页,含版本规格、价格与购买入口。

三、dnf 方式

在 TencentOS Server 中, 运行以下命令安装 Rust-toolset 软件包:

sudo dnf install rust-toolset

安装完毕后可通过如下指令检查是否正确安装了 Rust,此时能看到当前安装的版本号、哈希值和提交日期:

rustc -Vcargo -V

四、rustup 方式

除 dnf 安装方式以外,也可以通过 rustup 进行安装,简要安装指令如下:

sudo dnf install rustuprustup toolchain install stable

安装成功后,同样可执行 cargo -V 验证安装结果,此时能看到最新发布的稳定版本的版本号、提交哈希值和提交日期。

如果安装时出现如下报错,可尝试从 root 用户切换为普通用户后,重新执行 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'

五、Rust 使用

Cargo 是 Rust 编译器的构建工具和依赖项管理器。可以通过它声明有特定版本限制的依赖关系、解析完整的依赖关系图、下载包、构建以及测试整个 Rust 项目。Rust 开发者常用 Cargo 来管理 Rust 项目和获取所依赖的库。

七、项目类型

Cargo 通过执行 cargo new 指令,可以一键构建 Rust 项目,譬如新建一个经典项目:

cargo new hello_world –bin # 早期cargo必须设置–bin,以指定创建bin类型项目cargo new hello_world # 作用同上

新建一个 Rust 库:

cargo new –lib hello_lib

注意:说明:

八、项目结构

项目创建后,查看项目结构:

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)

九、运行项目

有两种方式可以运行你的 Rust 项目,第一种为手动编译后执行,另一种为一步到位。

十、分步运行

使用 Cargo 构建工具手动编译:

cargo build

然后可以看到项目下生成了 target 文件夹,且 target/debug 目录下生成了相应的可执行文件,手动执行即可。

# 查看文件类型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

十一、一步运行

对于 C/C++ 等开发者来说,先编译后执行的流程可能再熟悉不过。但是对于 Rust 开发者而言,Cargo 还提供了一键式运行的指令:

cargo run Compiling hello_world v0.1.0 (/root/test/hello_world) Finished dev [unoptimized + debuginfo] target(s) in 0.25s Running `target/debug/hello_world`

cargo run 首先对项目进行编译,然后再运行,因此它实际上等同于运行了两个分步指令,对于使用者而言可以简化操作。

十二、debug/release 模式

不论是手动分步还是一键运行,执行完毕后可以发现 target 下新增了 debug 目录,意味着默认运行的是 debug 模式。

tree -L 2.├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target ├── CACHEDIR.TAG └── debug # debug模式

在 debug 模式下,代码编译速度快,开发者可以流畅编译。但同时意味着运行速度会变慢,在发布时会带来负面影响。所以你也可以在发布时,显示指定为 release 模式:

cargo run –releasecargo build –release

同理,target 下会新增相应的 release 目录:

tree -L 2.├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target ├── CACHEDIR.TAG ├── debug └── release # release模式

十三、检查项目

当 Rust 项目臃肿之后,如果需要检验代码正确性,使用 cargo run 和 cargo build 会较为耗时。此时,可以通过 cargo check 对代码进行快速编译:

cargo check Finished dev [unoptimized + debuginfo] target(s) in 0.02s

此外,还可以运行测试 Rust 项目。如果项目中的测试较多,还支持指定运行单个测试、过滤部分测试等。但需注意的是返回类型必须是 () 或 Result<(), E> where E: Error 。

cargo test # debug 模式cargo test –release # release 模式

十四、项目文档

Cargo 还支持从源代码中提取注释以生成文档。但需要注意的是:

·仅为公共函数、变量和成员提取文档注释;

·所提取的注释,要使用三个斜线 /// 对其进行标记;

cargo doc –no-deps

十五、Rust 工具 rustfmt 介绍

rustfmt 是一个用于格式化 Rust 代码的工具,可以帮助开发者确保代码的整洁、一致性和可读性。使用 rustfmt 可以将 Rust 代码自动格式化为符合社区推荐的代码风格,提供一致的代码规范。 rustfmt 既可以独立使用,也可以与 Cargo 一起使用。

在开始使用 rustfmt 之前,请确保安装了 rustfmt 。如果尚未安装,可以通过运行以下命令轻松地安装它:

dnf install rustfmt

成功安装 rustfmt 后,你可以按照以下方式使用它:

十六、rustfmt 独立使用

使用 rustfmt 命令后,程序将根据默认配置规范格式化指定的 Rust 源文件。默认情况下, rustfmt 会将格式化过的内容直接输出到终端。

rustfmt src/test.rs

以原地覆盖方式格式化文件 若要让 rustfmt 直接修改源文件,以原地覆盖的方式格式化,可以使用 –emit 标志:

rustfmt –emit files src/test.rs

十七、同 Cargo 一起使用

rustfmt xx.rs 可以格式化指定文件,而使用 cargo fmt 命令则可以自动应用 rustfmt 格式化规则,为项目中所有的 .rs 文件进行格式化。

cargo fmt

如果要自定义格式化规则,你需要在项目根目录或任何父目录中创建一个名为 rustfmt.toml 的文件。在这个文件中,可以添加省略引号的键值对配置,例如:

max_width = 100reorder_imports = true

创建 rustfmt.toml 文件后, rustfmt 会自动应用其中的配置选项进行格式化。

十九、Cargo.toml 校验

修改 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.toml 文件后,未同步修改校验文件 .cargo-checksum.json 导致,在该 json 文件中找到 Cargo.toml 内容并修改为实际值即可。其余文件的校验错误同理。

# 将"Cargo.toml":"d6e10d3ec19f19b5a32cdcd159567ede9fa1e59ac13f446f0a1e40f99e7e6c1b"# 修改为 "Cargo.toml":"ea00aa6587f4f1793c296fd4a18c9989239e13df703c9254906b1c757714d440"

20、Cargo.lock 指定版本不匹配

Cargo.toml 中描述对于版本的要求,Cargo.lock 为根据编译时环境自动生成。

例如查看 gnome-tour-43.0 中的 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 update Updating gtk3 v0.4.8 -> v0.4.9

更新完毕后,即可正常编译。

21、附录

更多使用方法,请参见:

·Rust 产品文档

·Cargo 产品文档

·Rustfmt 配置文档

·rustup 产品文档

赞 (0)
上一篇 4天前
下一篇 4天前