2021-06-20 # Rust 開發環境建置 (以 Windows 為例) - Rust toolchain (安裝至 Program Files 目錄) - 支援 Rust 語法的編輯器 # Rust toolchain - 至 rust-lang.org 下載 [rustup-init.exe](https://www.rust-lang.org/tools/install) * 選擇 64-bit 版本 * 除非仍在使用 32 位元作業系統,才選擇下載 32-bit 版本。 - 執行 rustup-init,依照指示完成安裝… ## Microsoft C++ build tools - Rust 依賴 Microsoft C++ build tools 與其下列套件: * Windows 10 SDK * English language pack - 有兩種方式完成安裝 * **不需要 Visual Studio** (推薦) 1. 下載並執行 [Microsoft C++ build tools 安裝程式](https://visualstudio.microsoft.com/visual-cpp-build-tools/) 2. 勾選「使用 C++ 的桌面開發」 ![](https://i.imgur.com/cxAaAPI.png) 3. 語言套件改勾選「英文」 ![](https://i.imgur.com/IypjHa6.png) 4. 點選「安裝」進行安裝 * **有使用 Visual Studio** 1. 如果原本就打算使用 Visual Studio,則下載並執行 [Visual Studio 安裝程式](https://visualstudio.microsoft.com/downloads/); 或是已經安裝有 Visual Studio,則開啟其安裝程式。 2. 確認勾選下列選項 * 工作負載:使用 C++ 的桌面開發 * 語言套件:英文 3. 點選「安裝」進行安裝 (或「關閉」結束程式) :::spoiler 若未完成安裝 build tools 而執行 rustup-init,也會得到相關的提示。(點此展開) ``` Rust Visual C++ prerequisites Rust requires the Microsoft C++ build tools for Visual Studio 2013 or later, but they don't seem to be installed. The easiest way to acquire the build tools is by installing Microsoft Visual C++ Build Tools 2019 which provides just the Visual C++ build tools: https://visualstudio.microsoft.com/visual-cpp-build-tools/ Please ensure the Windows 10 SDK and the English language pack components are included when installing the Visual C++ Build Tools. Alternately, you can install Visual Studio 2019, Visual Studio 2017, Visual Studio 2015, or Visual Studio 2013 and during install select the "C++ tools": https://visualstudio.microsoft.com/downloads/ Install the C++ build tools before proceeding. If you will be targeting the GNU ABI or otherwise know what you are doing then it is fine to continue installation without the build tools, but otherwise, install the C++ build tools before proceeding. Continue? (y/N) ``` ::: ## Rust toolchain 完成前述步驟,再次執行 rustup-init,預期就能看見「Welcome to Rust!」的訊息。 ``` Welcome to Rust! This will download and install the official compiler for the Rust programming language, and its package manager, Cargo. Rustup metadata and toolchains will be installed into the Rustup home directory, located at: C:\Users\yipo\.rustup This can be modified with the RUSTUP_HOME environment variable. The Cargo home directory located at: C:\Users\yipo\.cargo This can be modified with the CARGO_HOME environment variable. The cargo, rustc, rustup and other commands will be added to Cargo's bin directory, located at: C:\Users\yipo\.cargo\bin This path will then be added to your PATH environment variable by modifying the HKEY_CURRENT_USER/Environment/PATH registry key. You can uninstall at any time with rustup self uninstall and these changes will be reverted. Current installation options: default host triple: x86_64-pc-windows-msvc default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation > ``` - Rust 預設將安裝在使用者目錄之下,例如: * `C:\Users\yipo\.rustup` 與 * `C:\Users\yipo\.cargo` - 若直接按下 enter 即可繼續完成安裝… 不過身為 強迫導向工程師,希望東西都能妥善收置在合適的地方。 * 如 應用程式,就該安裝在 Program Files 目錄, * 而不是 C: 根目錄,也不是使用者目錄。 - 這裡提供 **將 Rust 安裝在 Program Files 目錄** 的方式 1. 儲存以下內容為 rustup-init.bat,置於 rustup-init.exe 旁。 ``` @set USERPROFILE=%ProgramFiles%\Rust @cd /d %~dp0 rustup-init.exe @pause ``` 2. 以 系統管理員 身份執行 rustup-init.bat 3. 確認訊息當中的路徑,皆已改變至 Program Files,則按下 enter 完成安裝。 - 缺點:往後安裝套件也必須以 系統管理員 身份執行 - `error: no override and no default toolchain set` * 仍須設定 環境變數 `RUSTUP_HOME` 為 `C:\Program Files\Rust\.rustup` :::spoiler 安裝的過程 (點此展開) ``` info: profile set to 'default' info: default host triple is x86_64-pc-windows-msvc info: syncing channel updates for 'stable-x86_64-pc-windows-msvc' info: latest update on 2021-05-10, rust version 1.52.1 (9bc8c42bb 2021-05-09) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' 15.3 MiB / 15.3 MiB (100 %) 10.7 MiB/s in 1s ETA: 0s info: downloading component 'rust-std' 21.4 MiB / 21.4 MiB (100 %) 10.3 MiB/s in 2s ETA: 0s info: downloading component 'rustc' 59.6 MiB / 59.6 MiB (100 %) 9.4 MiB/s in 6s ETA: 0s info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' 15.3 MiB / 15.3 MiB (100 %) 1.2 MiB/s in 20s ETA: 0s info: installing component 'rust-std' 21.4 MiB / 21.4 MiB (100 %) 5.2 MiB/s in 12s ETA: 0s info: installing component 'rustc' 59.6 MiB / 59.6 MiB (100 %) 10.7 MiB/s in 5s ETA: 0s info: installing component 'rustfmt' info: default toolchain set to 'stable-x86_64-pc-windows-msvc' stable-x86_64-pc-windows-msvc installed - rustc 1.52.1 (9bc8c42bb 2021-05-09) Rust is installed now. Great! To get started you may need to restart your current shell. This would reload its PATH environment variable to include Cargo's bin directory (%USERPROFILE%\.cargo\bin). Press the Enter key to continue. ``` ::: # Visual Studio Code + Rust - 支援 Rust 語法的編輯器,這裡選擇 Visual Studio Code。 * 也是 [官方網站推薦](https://www.rust-lang.org/tools) 的第一個 - **安裝** 1. 下載並安裝 [Visual Studio Code](https://code.visualstudio.com/#alt-downloads) * 選擇 System Installer 也可改安裝於 Program Files 目錄 2. 尋找並安裝 [Rust 擴充功能](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust) * 若 Rust 安裝於 Program Files,則須以 系統管理員 身份執行 Visual Studio Code。 - **使用** * <kbd>ctrl</kbd> <kbd>\`</kbd> -- 開啟命令列以執行 `cargo` 等指令 * <kbd>shift</kbd> <kbd>alt</kbd> <kbd>f</kbd> -- 整理原始碼格式 {%hackmd @yipo/style %}