Z
ZHANK
编程在线教程Rust 教程环境搭建:rustup 与 Cargo
入门基础

环境搭建:rustup 与 Cargo

安装 rustup、cargo new/build/run、VS Code + rust-analyzer

环境搭建:rustup 与 Cargo

rustup 管理 Rust 工具链,cargo 是构建系统+包管理器——二者是 Rust 生态的基石。

学完本章你将: 掌握 rustup 安装、cargo new/build/run、VS Code 配置。


安装 Rust

bash
# 一行安装(Windows/macOS/Linux 通用)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Windows 也可以下载 .exe 安装包

# 验证
rustc --version
cargo --version

# 更新
rustup update

VS Code 配置

  1. 安装扩展:rust-analyzer(必装)
  2. 可选:CodeLLDB(调试)、Even Better TOML

Cargo 项目

bash
# 创建项目
cargo new hello_rust
cd hello_rust

# 项目结构
# hello_rust/
# ├── Cargo.toml    # 依赖和元数据
# └── src/
#     └── main.rs    # 入口

# 编译 + 运行
cargo run
# Hello, world!

# 只编译(不运行)
cargo build

# 发布编译(优化)
cargo build --release

# 检查语法(不编译——快)
cargo check

Cargo.toml

toml
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
# 添加依赖后 cargo build 自动下载