1. Rust 入门
1.1 安装 Rust
Linux 或 MacOS(不推荐):
curl -sSf https://sh.rustup.rs | sh
建议使用 Rustup 安装,Rustup 是 Rust 的版本管理工具:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Windows 可使用 winget
安装:
# Rustup
winget install Rustlang.Rustup
# MSVC 版本
winget install Rustlang.Rust.MSVC
# GNU 版本
winget install Rustlang.Rust.GNU
建议同时安装 Rustup 和 MSVC 版本。
MacOS 还可以使用 brew
安装:
brew install rustup
brew install rust
验证 Rust 是否安装成功:
rustc --version
1.2 命令行工具
rustup
是 Rust 的命令行工具,用于安装和管理 Rust 工具链。
更新 Rust:
rustup update
返回格式如下:
rustc 1.78.0 (9b00956e5 2024-04-29)
卸载 Rust:
rustup self uninstall
查看文档:
rustup doc
1.3 开发工具
1.3.1 VS Code
对于 VS Code,使用 Rust 语言需要安装插件,推荐安装以下插件:
# Rust 语言支持
code --install-extension rust-lang.rust-analyzer
# Rust 语法高亮
code --install-extension dustypomerleau.rust-syntax
# TOML 文件支持
code --install-extension tamasfe.even-better-toml
# 依赖检查
code --install-extension fill-labs.dependi
1.3.2 RustRover
也可以使用 RustRover 来开发 Rust 项目,这是 JetBrains 开发的 Rust 语言 IDE,对于个人用户免费。
你可以使用包管理器来安装 RustRover:
# Windows 安装
winget install JetBrains.RustRover
# MacOS 安装
brew install --cask rustrover
# Ubuntu 安装
sudo snap install rustrover --classic
也可以到官网下载最新版本来直接安装。
1.4 Hello World
编写 hello_world.rs
:
fn main() {
println!("Hello, world!");
}
编译运行:
rustc hello_world.rs
./hello_world
1.5 Cargo
一般情况不推荐直接使用 rustc
编译大形项目的 Rust 源代码,而是使用 Cargo 来管理项目。
Cargo 是 Rust 的构建系统和包管理工具。
创建项目:
cargo new hello_cargo
cd hello_cargo
目录中包含类似于如下内容的配置文件 Cargo.toml
:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]
其中 [package]
是项目的元数据,一些常见的字段有:
name
:项目名称version
:项目版本authors
:作者edition
:Rust 版本
[dependencies]
是项目的依赖。
在 Rust 中,一般第三方库被称为 crate,可以在 crates.io 上查找常见的第三方库。
构建项目:
cargo build
第一次执行 cargo build
时,Cargo 会下载并编译项目的依赖,并生成 Cargo.lock
锁文件。
运行项目:
cargo run
检查项目:
cargo check
发布项目:
cargo build --release
发布模式会对代码进行优化,编译时间更长,但运行速度更快。生成的可执行文件位于 target/release
目录下。
清理项目:
cargo clean
这会删除 target
目录,清理所有编译生成的文件。
更新依赖:
cargo update
这会更新 Cargo.lock
文件中的依赖版本,但会遵循 Cargo.toml
中指定的版本约束。
查看文档:
cargo doc --open
这会生成项目及其所有依赖的文档,并在浏览器中打开。
1.6 Rust 版本管理
Rust 使用 Edition(版次)来管理语言的演进。Edition 允许 Rust 引入不兼容的更改,同时保持向后兼容性。
当前主要的 Edition 包括:
- Rust 2015(第一版)
- Rust 2018
- Rust 2021(当前稳定版推荐)
- Rust 2024(即将发布)
在 Cargo.toml
中可以指定使用的 Edition:
[package]
edition = "2021"
不同 Edition 之间可以互操作,可以在同一个项目中使用不同 Edition 的 crate。
1.7 常用 Cargo 命令
以下是一些常用的 Cargo 命令:
命令 | 说明 |
---|---|
cargo new | 创建新项目 |
cargo init | 在当前目录初始化项目 |
cargo build | 构建项目 |
cargo run | 运行项目 |
cargo check | 快速检查代码是否能编译 |
cargo test | 运行测试 |
cargo doc | 生成文档 |
cargo publish | 发布到 crates.io |
cargo install | 安装二进制 crate |
cargo clean | 清理生成的文件 |
cargo update | 更新依赖 |
cargo tree | 显示依赖树 |
cargo fmt | 格式化代码 |
cargo clippy | 运行 linter 检查代码 |
使用 cargo --help
可以查看更多命令。
1.8 Rust 代码风格
Rust 官方提供了代码风格指南,推荐使用 rustfmt
工具来格式化代码:
cargo fmt
安装 rustfmt
:
rustup component add rustfmt
Rust 代码风格的一些要点:
- 使用 4 个空格缩进,不使用 Tab
- 函数名和变量名使用蛇形命名法(
snake_case
) - 类型名和 Trait 名使用驼峰命名法(
CamelCase
) - 常量使用全大写加下划线(
SCREAMING_SNAKE_CASE
) - 每行代码建议不超过 100 个字符
1.9 Rust Linter
Clippy 是 Rust 的 linter 工具,可以帮助你写出更符合习惯的 Rust 代码:
cargo clippy
安装 Clippy:
rustup component add clippy
Clippy 会检查代码中的常见错误和不良实践,并提供改进建议。它包含数百个检查规则,涵盖了性能、正确性、风格等多个方面。
1.10 Rust Playground
Rust Playground 是一个在线的 Rust 开发环境,可以在浏览器中编写和运行 Rust 代码:https://play.rust-lang.org/
它支持:
- 编译和运行代码
- 分享代码片段
- 查看生成的汇编代码
- 使用标准库和一些流行的 crate
这是学习 Rust 和快速测试代码的好工具。