更新於 2024.07.24
Archlinux 軟體庫安裝
從 Archlinux 軟體庫中安裝 rust 會安裝 rustc 和 Cargo 的最新穩定版本。
rustc 是編譯器,Cargo 則是整合了 rustc 和包管理工具。
sudo pacman -S rust
#
# 軟體包 (1) rust-1:1.78.0-1
#
# 總計下載大小: 61.50 MiB
# 總計安裝大小: 251.91 MiB
交叉編譯
嵌入式系統是 Rust 的願景之一,開發嵌入式系統時就需要有能跨系統或跨架構的編譯器,此編譯器又稱為交叉編譯器。
交叉編譯需要目標三元組的支持,需要下載目標的系統架構、供應商、作業系統和環境的工具鏈。
下載的工具鏈會存放於 /path/to/.rustup/toolchains
目錄。
有此需求可依官方建議安裝 rustup
— rust 版本及工具鏈管理工具。
閱讀更多:
- What is a “default host triple” in Rust? //stackoverflow.com
- Cross-Compilation //gnu.org
- Cross-compilation using Clang — Clang 20.0.0git documentation //llvm.org
官方安裝
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
該安裝腳本會自動為當前電腦配置安裝選項如:
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable
profile: default
modify PATH variable: yes
下載的文件會放於$HOME/.rustup
(rustup 元資料和工具鏈)
和 $HOME/.cargo
(cargo, rustc, rustup 和其他命令) 兩目錄下。
該互動式腳本可以透過對話修改選項,也可以透過環境變數和參數修改,如下:
export RUSTUP_HOME=/path/to/.rustup
export CARGO_HOME="/path/to/.cargo"
sh <(curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs) \
--default-toolchain nightly \
--no-modify-path \
-y
export PATH="$PATH:$CARGO_HOME/bin"
檢查是否安裝成功
檢查 rustc 編譯器是否安裝成功
rustc --version
# rustc 1.82.0-nightly (8bfcae730 2024-07-23)
測試 Cargo 是否安裝成功
cargo new hello_cargo
# Creating binary (application) `hello_cargo` package
# note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
cargo run
# Compiling hello_cargo v0.1.0 (/path/to/hello_cargo)
# Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
# Running `target/debug/hello_cargo`
# Hello, world!