安裝 Rust 與開發環境

最後更新


用官方的 rustup 安裝與更新 Rust,認識 toolchain 與 component,確認 rustc/cargo 版本,再替編輯器裝上 rust-analyzer。

用 rustup 安裝


rustup 是 Rust 官方的工具鏈管理員(toolchain installer)。它不是 Rust 編譯器本身,而是負責下載、更新、切換編譯器版本的工具,類似 Node.js 的 nvm 或 Python 的 pyenv。裝好 rustup,就會一併得到:
工具角色
rustcRust 編譯器
cargo建置工具兼套件管理員,日常最常用
rustup管理上面這些工具的版本與元件

不建議用作業系統的套件管理員(如 apt)安裝 Rust,那裡的版本通常較舊,也無法用 rustup 切換版本。

安裝 rustup


macOS 與 Linux 在終端機執行官方安裝指令,依提示選擇預設安裝即可。Windows 則到 rustup.rs 下載 rustup-init.exe 執行,安裝程式會提示你一併安裝 Visual Studio 的 C++ 建置工具(Rust 需要它的連結器)。macOS 若出現找不到 cc 的錯誤,先執行 xcode-select --install;Linux 則需安裝 gcc 或 build-essential。
  1. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

工具會裝在 ~/.cargo/bin(Windows 為 %USERPROFILE%\.cargo\bin),安裝程式會把它加進 PATH。裝完後開一個新的終端機,確認 rustup 能執行:
範例:
  1. rustup --version

輸出:
  1. rustup 1.29.3 (edbc831ce 2026-11-02)
  2. info: This is the version for the rustup toolchain manager, not the rustc compiler.
  3. info: the currently active `rustc` version is `rustc 1.100.0 (0ef1bc386 2026-11-09)`

注意:如果出現 command not found,通常是舊的終端機還沒讀到新的 PATH。重開終端機,或執行 source "$HOME/.cargo/env"。

檢查與更新


Rust 每 6 週發布一個新的穩定版。rustup check 只檢查有沒有新版、不會下載;rustup update 則會實際下載並更新所有已安裝的 toolchain 與 rustup 本身。各版本的變更見 版本紀錄。
範例:
  1. rustup check

輸出:
  1. stable-aarch64-apple-darwin - up to date: 1.100.0 (0ef1bc386 2026-11-09)
  2. rustup - up to date : 1.29.3

aarch64-apple-darwin 是平台名稱(Apple Silicon 的 macOS),在 Intel Linux 上會是 x86_64-unknown-linux-gnu,以你的機器為準。

toolchain 與 component


toolchain(工具鏈)是一整套特定版本的 rustc、cargo、標準庫。component(元件)是可以額外加裝到 toolchain 上的工具,例如排版工具 rustfmt。

發布頻道與預設 toolchain


Rust 有三個發布頻道(channel):
頻道說明適合
stable每 6 週一版的穩定版一般開發,本書全部使用它
beta下一個 stable 的候選版想提前測試相容性
nightly每天建置,含未穩定功能需要實驗性功能時

rustup toolchain list 列出已安裝的 toolchain,rustup show active-toolchain 顯示目前目錄實際使用的是哪一個。要安裝其他頻道用 rustup toolchain install nightly,臨時用它編譯可以寫 cargo +nightly build。專案根目錄放 rust-toolchain.toml 可以固定該專案使用的版本,讓團隊成員一致。
範例:
  1. rustup toolchain list
  2. rustup show active-toolchain

輸出:
  1. stable-aarch64-apple-darwin (active, default)
  2. stable-aarch64-apple-darwin (default)

列出與新增 component


rustup component list --installed 列出已安裝的元件,rustup component add 名稱 新增。最常用的是排版工具 rustfmt 與 lint 工具 clippy,用法見 cargo 指令總覽。用預設設定安裝時兩者都已包含;若當初選了 minimal 安裝設定檔(profile),就需要手動加裝。已裝過的元件再 add 一次不會出錯,只會告知已是最新。
範例:
  1. rustup component add rustfmt clippy
  2. rustup component list --installed

輸出:
  1. info: component rustfmt is up to date
  2. info: component clippy is up to date
  3. cargo-aarch64-apple-darwin
  4. clippy-aarch64-apple-darwin
  5. rust-docs-aarch64-apple-darwin
  6. rust-src
  7. rust-std-aarch64-apple-darwin
  8. rustc-aarch64-apple-darwin
  9. rustfmt-aarch64-apple-darwin

rust-docs 是離線版官方文件,rustup doc --std 會用瀏覽器開啟本機的標準庫文件;rust-src 是標準庫原始碼,rust-analyzer 需要它來提供標準庫的跳轉與補全。

確認版本


rustc 與 cargo 版本


rustc --version 與 cargo --version 是回報問題、查文件時最先要確認的資訊。rustc -vV(verbose)會多列出 commit、平台(host)與使用的 LLVM 版本。本書所有範例都以下面這個版本實際執行。
範例:
  1. rustc --version
  2. cargo --version
  3. rustc -vV

輸出:
  1. rustc 1.100.0 (0ef1bc386 2026-11-09)
  2. cargo 1.100.0 (3acc79c0c 2026-11-05)
  3. rustc 1.100.0 (0ef1bc386 2026-11-09)
  4. binary: rustc
  5. commit-hash: 0ef1bc386059596dada8fb65b50425aab2ea637a
  6. commit-date: 2026-11-09
  7. host: aarch64-apple-darwin
  8. release: 1.100.0
  9. LLVM version: 23.1.5

書中標註「Rust 1.xx 起」的功能,表示要那個版本以上的 rustc 才能用。各版本的變動見 版本紀錄,本版的內容見 Rust 1.100。

編譯一支小程式確認環境


版本號印得出來,不代表連結器等外部工具也就緒。最後用一支小程式實際編譯執行一次。把下面的內容存成 check.rs,執行 rustc --edition 2024 check.rs && ./check。std::env::consts 提供目前編譯目標的作業系統與 CPU 架構,見 std::env。
範例:
  1. fn main() {
  2. println!("工具鏈可以正常編譯了!");
  3. println!("目標平台:{} / {}", std::env::consts::OS, std::env::consts::ARCH);
  4. println!("指標寬度:{} 位元", usize::BITS);
  5. }

輸出:
  1. 工具鏈可以正常編譯了!
  2. 目標平台:macos / aarch64
  3. 指標寬度:64 位元

這是 64 位元 Apple Silicon macOS 上的結果,其他平台會印出不同的值。若這一步出現 linker 'cc' not found 之類的錯誤,回到安裝一節補裝 C 建置工具。程式本身的寫法見 第一個 Rust 程式。

編輯器:rust-analyzer


rust-analyzer 是官方的語言伺服器(language server),讓編輯器支援 Rust 的自動補全、跳到定義、即時顯示編譯錯誤、型別提示、重新命名等功能。它不綁定特定編輯器,VS Code、Zed、Neovim、Helix、Emacs 都能用。JetBrains 系列則使用自家的 RustRover 或 Rust 外掛。

安裝 rust-analyzer


最簡單的方式是在 VS Code 擴充功能市集搜尋並安裝 rust-analyzer,它會自帶對應的伺服器程式。使用其他編輯器時,可以用 rustup 安裝 rust-analyzer 元件(Rust 1.64 起可在 stable 頻道取得),讓它的版本跟著 toolchain 一起更新。下面先查詢元件狀態,沒有標 (installed) 表示尚未安裝:
範例:
  1. rustup component list | grep -E "rust-analyzer|rust-src"

輸出:
  1. rust-analyzer-aarch64-apple-darwin
  2. rust-src (installed)

安裝指令是 rustup component add rust-analyzer rust-src。
注意:rust-analyzer 只在 cargo 專案(有 Cargo.toml 的資料夾)裡能完整運作。用 VS Code 開單一 .rs 檔案時,補全與錯誤提示會很有限;請用 cargo new 建立專案後開啟整個資料夾。

重點整理


  • 用 rustup 安裝 Rust,會一併得到 rustc、cargo、rustup;不要用系統套件管理員裝
  • rustup check 檢查新版、rustup update 更新;Rust 每 6 週發布一個 stable 版
  • toolchain 是整套特定版本的工具,頻道分 stable、beta、nightly,一般開發用 stable
  • component 是加裝在 toolchain 上的工具,常用 rustfmt、clippy、rust-src、rust-analyzer
  • 用 rustc --version、cargo --version 確認版本,再實際編譯一支小程式確認連結器正常
  • 編輯器裝 rust-analyzer,並以 cargo 專案資料夾開啟

資料來源