|
| 1 | +# Rust 快速入门 |
| 2 | + |
| 3 | +## Rust 环境安装 |
| 4 | + |
| 5 | +### 开发环境 |
| 6 | + |
| 7 | +你可以从选择以下任意一条路径,搭建 Rust 开发环境。 |
| 8 | + |
| 9 | +- Visual Studio + Rust Analyzer: 需要在本地安装好 `rustup`,Rust Analyzer插件能够在未编译的阶段提供代码报错与补全。 |
| 10 | + |
| 11 | +- JetBrains RustRover: JetBrains 提供现成的 Rust 编程环境,内置 Cargo,提供便捷的代码补全与编译运行。 |
| 12 | + |
| 13 | +- [Rust Playground](https://play.rust-lang.org/): 在线 Rust 编程环境。 |
| 14 | + |
| 15 | +### 编译工具链配置 |
| 16 | + |
| 17 | +#### 安装 rustup |
| 18 | + |
| 19 | +在 Linux 或者 macOS 命令行环境下运行下述命令: |
| 20 | + |
| 21 | +```bash |
| 22 | +$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
| 23 | +``` |
| 24 | + |
| 25 | +如果显示错误 `Commmand 'curl' not found`,运行下述命令: |
| 26 | + |
| 27 | +```bash |
| 28 | +$ sudo apt update |
| 29 | +$ sudo apt install -y curl |
| 30 | +``` |
| 31 | +安装完成后,重新运行上面的 rustup 安装命令。 |
| 32 | + |
| 33 | +如果网络正常,rustup 安装命令会提示下述信息: |
| 34 | + |
| 35 | +```bash |
| 36 | +Current installation options: |
| 37 | + |
| 38 | + |
| 39 | + default host triple: aarch64-apple-darwin |
| 40 | + default toolchain: stable (default) |
| 41 | + profile: default |
| 42 | + modify PATH variable: yes |
| 43 | + |
| 44 | +1) Proceed with installation (default) |
| 45 | +2) Customize installation |
| 46 | +3) Cancel installation |
| 47 | +> |
| 48 | +``` |
| 49 | + |
| 50 | +回车以后,它就会安装 Rust 所需要的编译器(rustc)、标准库(rust-std)等等。 |
| 51 | + |
| 52 | +!!! note "Windows 安装 rustup" |
| 53 | + |
| 54 | + 前往 https://forge.rust-lang.org/infra/other-installation-methods.html,在 **Other ways to install rustup** 中,找到: |
| 55 | + |
| 56 | + - On Windows, download and run rustup-init.exe. |
| 57 | + |
| 58 | + 下载并运行 **rustup-init.exe**。 |
| 59 | + |
| 60 | + |
| 61 | +#### 测试 rust 工具链 |
| 62 | + |
| 63 | +下载完成后,命令行会提示下述信息: |
| 64 | + |
| 65 | +```bash |
| 66 | +Rust is installed now. Great! |
| 67 | + |
| 68 | +To get started you may need to restart your current shell. |
| 69 | +This would reload your PATH environment variable to include |
| 70 | +Cargo's bin directory ($HOME/.cargo/bin). |
| 71 | +
|
| 72 | +To configure your current shell, run: |
| 73 | +source $HOME/.cargo/env |
| 74 | +``` |
| 75 | +
|
| 76 | +此时 shell 配置已更新。重启终端,PATH 环境变量就会包括 rustup 安装路径。如果想要在当前终端使用 rust 编译器,你也可以直接运行: |
| 77 | +
|
| 78 | +```bash |
| 79 | +$ source $HOME/.cargo/env |
| 80 | +``` |
| 81 | +
|
| 82 | +完成后,可以通过下述检查 `rustc` 是否成功安装: |
| 83 | +
|
| 84 | +```bash |
| 85 | +$ rustc --version |
| 86 | +``` |
| 87 | +
|
| 88 | +如果正常输出`rustc`版本信息,说明安装成功了。 |
| 89 | +
|
| 90 | +## Hello World! |
| 91 | +
|
| 92 | +### 直接使用 rustc 编译 |
| 93 | +
|
| 94 | +在当前目录下,通过下述命令创建项目。 |
| 95 | +
|
| 96 | +```bash |
| 97 | +$ touch hello.rs |
| 98 | +``` |
| 99 | +
|
| 100 | +在 `hello.rs` 中,写入如下内容: |
| 101 | +
|
| 102 | +```rust |
| 103 | +fn main() { |
| 104 | + println!("Hello world!"); |
| 105 | +} |
| 106 | +``` |
| 107 | +
|
| 108 | +使用 `rustc` 编译,运行 `hello` 可执行程序并得到下述结果。 |
| 109 | +
|
| 110 | +```bash |
| 111 | +$ rustc hello.rs |
| 112 | +$ ./hello |
| 113 | +Hello world! |
| 114 | +``` |
| 115 | +
|
| 116 | +### 使用 Cargo 包管理器 |
| 117 | +
|
| 118 | +你也可以通过 Cargo 包管理器来创建一个新项目。 |
| 119 | +
|
| 120 | +```bash |
| 121 | +$ Cargo new hello |
| 122 | +``` |
| 123 | +
|
| 124 | +此时 Cargo 会在你的当前目录下生成一个 `hello` 文件夹,进入文件夹,通过 `tree` 可以查看项目结构。 |
| 125 | +
|
| 126 | +```bash |
| 127 | +$ cd hello |
| 128 | +$ tree |
| 129 | +. |
| 130 | +├── Cargo.toml |
| 131 | +└── src |
| 132 | + └── main.rs |
| 133 | +``` |
| 134 | +
|
| 135 | +其中 `Cargo.toml` 为项目的配置文件,存放了项目的相关信息、依赖项等信息。 |
| 136 | +
|
| 137 | +由 Cargo 生成的 `main.rs` 会默认生成上述 `Hello world!` 程序,直接编译运行即可。 |
| 138 | +
|
| 139 | +```bash |
| 140 | +$ cargo run |
| 141 | + Compiling hello v0.1.0 (/.../hello) |
| 142 | + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s |
| 143 | + Running `target/debug/hello` |
| 144 | +Hello, world! |
| 145 | +``` |
| 146 | +
|
| 147 | +## Rust 项目结构 |
| 148 | +
|
| 149 | +Rust 采用**模块化**的项目结构,每个文件/文件夹都被视为一个模块,文件内部还可以定义多个/多重模块。 |
| 150 | +
|
| 151 | +可以通过 `use` 来访问模块。 |
| 152 | +
|
| 153 | +```rust |
| 154 | +// Consume that your src directory has main.rs and calendar.rs |
| 155 | +mod calendar; |
| 156 | +use calendar::get_day; |
| 157 | +
|
| 158 | +mod body { |
| 159 | + pub mod mouth { |
| 160 | + pub fn use_mouth() {} |
| 161 | + } |
| 162 | +} |
| 163 | +
|
| 164 | +mod language { |
| 165 | + pub mod Chinese{ |
| 166 | + pub fn speak() { |
| 167 | + use crate::body; |
| 168 | + body::mouth::use_mouth(); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +fn main() { |
| 174 | + get_day(); |
| 175 | + language::Chinese::speak(); |
| 176 | +} |
| 177 | +``` |
| 178 | +
|
| 179 | +Rust 以**包(packages)**为项目单位,用户可以通过包构建、测试、分享**箱(crates)**(也成单元包,对应一个库或可执行文件)。用户也可以实现自己的箱,通过在 `Cargo.toml` 的 `[dependencies]` 中配置箱的对应路径,即可在程序中通过 `use` 使用自己的箱。 |
| 180 | +
|
| 181 | +## Cargo 包管理器 |
| 182 | +
|
| 183 | +Cargo 是 Rust 的一个非常便捷好用的**包管理器(*package manager*)**,许多 Rust 程序员都在使用 Cargo 来管理他们的 Rust 项目。Cargo 能够为你处理很多任务,比如**构建代码**、**下载代码所依赖的库**,以及**构建这些库**。(代码所需要的库通常被称为**依赖项**) |
| 184 | +
|
| 185 | +### 创建项目 |
| 186 | +
|
| 187 | +```bash |
| 188 | +$ cargo new <your-project-name> |
| 189 | +``` |
| 190 | +
|
| 191 | +### 编译项目 |
| 192 | +
|
| 193 | +```bash |
| 194 | +$ cargo run # 编译并运行项目 |
| 195 | +$ cargo build # 仅编译,不运行 |
| 196 | +$ cargo check #不编译,检查错误 |
| 197 | +``` |
| 198 | +
|
| 199 | +如果希望发布项目,请使用 `cargo build --release` 对其进行项目优化。(此命令将在 target/release 而不是 target/debug 中创建可执行文件) |
| 200 | +
|
| 201 | +### 添加依赖 |
| 202 | +
|
| 203 | +```bash |
| 204 | +$ cargo add <crate> |
| 205 | +``` |
| 206 | +
|
| 207 | +通过该命令添加的依赖将会同步至 `Cargo.toml`中。 |
| 208 | +
|
| 209 | +也可以直接在 `Cargo.toml` 中写入依赖,随后通过 `cargo check` 或 `cargo build` 直接构建。 |
| 210 | +
|
| 211 | +### 自动格式化 |
| 212 | +
|
| 213 | +```bash |
| 214 | +$ cargo fmt |
| 215 | +``` |
| 216 | +
|
| 217 | +Cargo 提供了代码风格的自动统一,通过上述指令可快速实现缩进和换行的自动对齐。 |
| 218 | +
|
| 219 | +### 运行测试 |
| 220 | +
|
| 221 | +Cargo 可以通过 `#[test]` 标注测试函数,通过 `cargo test` 命令进行单元测试。 |
| 222 | +
|
| 223 | +```rust |
| 224 | +fn multiple(x: i32, y: i32) -> i32 { |
| 225 | + x * y |
| 226 | +} |
| 227 | +
|
| 228 | +#[test] |
| 229 | +fn multiple_test() { |
| 230 | + assert_eq!(2, multiple(1, 2)); |
| 231 | +} |
| 232 | +
|
| 233 | +fn main() {} |
| 234 | +``` |
| 235 | +
|
| 236 | +运行 `cargo test` 并查看结果。 |
| 237 | +
|
| 238 | +```bash |
| 239 | +$ cargo test |
| 240 | + Compiling Test v0.1.0 (/.../my_project) |
| 241 | + Finished `test` profile [unoptimized + debuginfo] target(s) in 0.45s |
| 242 | + Running unittests src/main.rs (target/debug/deps/my_project-5ab9f918a19e3cb1) |
| 243 | +
|
| 244 | +running 1 test |
| 245 | +test multiple_test ... ok |
| 246 | +
|
| 247 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s |
| 248 | +``` |
| 249 | +
|
| 250 | +可以通过 `cargo test -- <test-name> --test-threads=1` 来指定测试和同时运行的测试数。 |
| 251 | +
|
| 252 | +!!! note "Tips" |
| 253 | + 通过 `#[cfg(test)]` 可以实现测试代码仅在测试模式下编译。 |
| 254 | +
|
| 255 | +更多 Cargo 的使用方法,请参考 Cargo 的[官方文档](https://doc.rust-lang.org/cargo/) |
| 256 | +
|
| 257 | +第三方包可以通过该链接查阅:https://crates.io |
0 commit comments