# Rust 环境搭建与 Hello World

![Rust 基础：所有权与内存](https://img.zhaojq.top/20260804234640617.png "Rust 基础：所有权与内存")

Rust 的工具链靠 **rustup** 管理，**Cargo** 是它自带的构建与包管理工具，几乎你之后做的每件事都离不开它。

## 安装工具链

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
rustc --version   # 编译器
cargo --version   # 包管理/构建
```

> 国内网络慢？装之前设镜像：`export RUSTUP_DIST_SERVER=https://rsproxy.cn`。

## 第一个项目

```bash
cargo new hello_rust
cd hello_rust
cargo run
```

`cargo new` 会生成一个标准目录：

```
hello_rust/
├── Cargo.toml   # 项目清单（依赖写这里）
└── src/
    └── main.rs  # 入口
```

## Hello World

`src/main.rs` 默认内容：

```rust
fn main() {
    println!("Hello, world!");
}
```

`fn main()` 是程序入口；`println!` 是宏（带 `!`），负责向终端打印并换行。

## 你该记住的

- `cargo build` 编译，`cargo run` 编译并运行，`cargo check` 只检查不生成二进制（更快）。
- 改依赖就编辑 `Cargo.toml` 的 `[dependencies]`，再 `cargo build` 自动拉取。
- 每一次编译报错都是 Rust 在帮你挡 bug，别慌，读红字。

下一章我们碰 Rust 最特别的东西——**所有权**。

➡️ [2. 变量、所有权与借用](rust-02-ownership.md)

