rustbook/01.md

196 lines
4.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 第一章 Rust初体验开启系统编程新纪元
## 1.1 Rust语言概览
Rust是一门专注于**安全、并发和性能**的现代系统编程语言。由Mozilla研究院开发自2015年稳定版发布以来迅速成为开发者最喜爱的语言之一。其独特的所有权系统在编译期消除内存错误零成本抽象提供C/C++级别的性能,而现代化的工具链则让开发体验异常流畅。
## 1.2 安装Rust工具链
### 跨平台安装指南
Rust提供了简单的一键安装脚本支持Windows、macOS和Linux
```bash
# 在终端中执行安装命令
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
安装过程包含:
1. Rust编译器rustc
2. 包管理器Cargo
3. 工具链管理器rustup
4. 标准库文档
### 验证安装
安装完成后,重启终端并运行:
```bash
rustc --version
cargo --version
rustup --version
```
正常输出类似:
```
rustc 1.78.0 (9b00956e5 2024-04-29)
cargo 1.78.0 (54d8815d0 2024-03-26)
rustup 1.27.2 (xxx)
```
## 1.3 第一个Rust程序Hello, Rust!
### 创建项目
使用Cargo创建新项目
```bash
cargo new hello_rust
cd hello_rust
```
项目结构:
```
hello_rust/
├── Cargo.toml # 项目配置和依赖声明
└── src/
└── main.rs # 程序入口文件
```
### 编写代码
打开`src/main.rs`文件,输入:
```rust
fn main() {
println!("Hello, Rust!");
}
```
代码解析:
- `fn main()`:程序入口函数
- `println!`:宏调用(注意感叹号标识)
- 行尾分号`;`:语句结束符
### 运行程序
在项目根目录执行:
```bash
cargo run
```
输出结果:
```
Compiling hello_rust v0.1.0 (/path/to/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
Running `target/debug/hello_rust`
Hello, Rust!
```
## 1.4 CargoRust的瑞士军刀
Cargo不仅是构建工具还是包管理器和项目脚手架
| 命令 | 功能 | 示例 |
|------|------|------|
| `cargo new` | 创建新项目 | `cargo new project_name` |
| `cargo build` | 编译项目 | `cargo build --release` |
| `cargo run` | 编译并运行 | `cargo run` |
| `cargo check` | 快速检查错误 | `cargo check` |
| `cargo update` | 更新依赖 | `cargo update` |
| `cargo doc` | 生成文档 | `cargo doc --open` |
## 1.5 探索Rust文档生态系统
### 官方文档门户
[Rust官方文档](https://www.rust-lang.org/learn) 包含:
- 《The Rust Programming Language》"The Book"
- 《Rust by Example》
- 《The Cargo Book》
- 《The rustc Book》
### 本地文档查看
安装时自带的离线文档可通过命令打开:
```bash
rustup doc
```
### 标准库文档
访问 [std](https://doc.rust-lang.org/std/) 或本地运行:
```bash
cargo doc --open --package std
```
### 第三方库文档
在[Crates.io](https://crates.io)找到库后,文档通常位于:
- 库的GitHub页面
- [docs.rs](https://docs.rs)自动生成的文档
### 文档注释示例
Rust支持特殊的文档注释
```rust
/// 计算两个数的和
///
/// # 示例
///
/// ```
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
fn add(a: i32, b: i32) -> i32 {
a + b
}
```
生成文档:
```bash
cargo doc --open
```
## 1.6 开发环境配置建议
### IDE选择
- **VS Code** + rust-analyzer扩展
- **IntelliJ IDEA** + Rust插件
- **CLion**专业Rust支持
### 实用工具
1. `rustfmt`:自动格式化代码
```bash
cargo fmt
```
2. `clippy`:代码质量检查
```bash
cargo clippy
```
## 1.7 常见问题排错
**Q安装后无法识别rustc命令**
A尝试重启终端或执行`source $HOME/.cargo/env`
**QCargo下载依赖慢**
A配置国内镜像源在`~/.cargo/config`添加):
```toml
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
```
**QWindows上编译失败**
A可能需要安装Visual Studio Build Tools或MinGW
## 1.8 本章小结
本章完成了:
- ✅ Rust开发环境搭建
- ✅ 第一个Rust程序创建和运行
- ✅ Cargo基础使用
- ✅ Rust文档资源探索
> "任何伟大的旅程都始于第一步。通过Hello, Rust!,你已踏入系统编程的新世界。"
**下一章预告**深入Rust语言核心——所有权系统与基本数据类型掌握Rust独特的内存管理哲学。
---
> 环境信息本书示例基于Rust 1.78稳定版编写所有代码均通过cargo 1.78测试验证。