Skip to content

Instantly share code, notes, and snippets.

@KmolYuan
Last active August 22, 2022 01:34
Show Gist options
  • Save KmolYuan/46b2b852c15ac87aa1fc99c7500a5dfc to your computer and use it in GitHub Desktop.
Save KmolYuan/46b2b852c15ac87aa1fc99c7500a5dfc to your computer and use it in GitHub Desktop.
Install Rust for Msys2.

在 Msys2 上安裝最新版 Rust

  1. 安裝 Msys2:https://www.msys2.org/

  2. 安裝後啟動 msys2.exe 執行以下命令安裝 GNU toolchain。(如官方說明所示)

    pacman -S --needed base-devel mingw-w64-x86_64-toolchain
  3. gcc 等指令的位置加到環境變數 Path,如 C:\msys64\mingw64\bin

  4. https://www.rust-lang.org/tools/install 下載 Rust installer,並執行。

    這時會出現當前設定和 3 個選項。1 是套用配置並安裝;2 是修改配置;3 是取消。 選擇 2 來修改配置,default 的部份可以按 enter 跳過。

    其中 host triple 設定成 x86_64-pc-windows-gnu,其餘可以自選。 而詢問修改環境變數時選 Y

  5. 在 cmd 命令提示字元測試 rustup --helpcargo --help 指令是否可以執行,可能需要重新開機。

  6. rustup toolchain install nightly 可以安裝 nightly 版本的工具鏈(推薦在 rustfmt 使用)。

  7. rustup update 可以在未來直接升級整套工具鏈的版本。可以關注 Rust changelog

Cargo 的指令如下:

# 建立新的 crate,預設為 bin,加上 --lib 變成 lib crate
cargo new [CRATE]
# 初始化當前目錄成為 crate
cargo init
# 編譯,加上 --release 用 release 模式編譯
cargo build
# 編譯並執行 bin crate
cargo run
# 測試
cargo test
# 產生 API 手冊
cargo doc
# 清除編譯目錄 target
cargo clean
# 更新 dependencies 的版本,建議編譯前先執行 clean
cargo update
# 程式碼 lint
cargo clippy
# 排版
cargo fmt
# 用 nightly 工具鏈
cargo +nightly [COMMAND]
# 安裝 Rust 工具
cargo install [CRATE]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment