Rust 构建工具链配置指南
一 安装与基础配置
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh;Windows 下载并运行 rustup-init.exe。完成后执行 rustc --version、cargo --version 验证。建议将 ~/.cargo/bin(Windows 为 %USERPROFILE%.cargo\bin)置于 PATH 环境变量最前,确保调用的是 rustup 代理。常用:rustup update 升级;rustup show 查看当前激活工具链与组件;rustup toolchain list 列出已安装工具链。二 项目级工具链与组件锁定
[toolchain]
channel = "stable" # 或 "nightly-2025-01-10"(钉日期)
components = ["rustfmt", "clippy"] # 按需增减
targets = ["wasm32-unknown-unknown"]
profile = "minimal" # minimal/default/complete
rustup override set nightly-2025-01-10;取消用 rustup override unset。cargo +nightly build(显式选择通道);rustup run nightly cargo test(本次命令用 nightly);RUSTUP_TOOLCHAIN=nightly cargo test(环境变量方式,便于脚本)。三 交叉编译与链接器配置
rustup target add wasm32-unknown-unknown
rustup target add aarch64-unknown-linux-gnu
rustup target add x86_64-pc-windows-gnu
cargo build --release --target wasm32-unknown-unknown
rustup target add x86_64-pc-windows-msvcsudo apt install lldcargo install xwin,然后接受许可并安装到目录(如 ~/.xwin)[target.x86_64-pc-windows-msvc]
linker = "lld"
rustflags = [
"-Lnative=/home/$USER/.xwin/crt/lib/x86_64",
"-Lnative=/home/$USER/.xwin/sdk/lib/um/x86_64",
"-Lnative=/home/$USER/.xwin/sdk/lib/ucrt/x86_64"
]
cargo build --target x86_64-pc-windows-msvc --release四 构建与开发环境优化
rustfmt(格式化)、clippy(静态检查)、rust-analyzer(LSP,IDE 智能)、llvm-tools-preview(配合 cargo-binutils 做 objdump/size 等)。安装示例:rustup component add rustfmt clippy rust-analyzer llvm-tools-preview。--profile minimal 减少下载与体积:rustup toolchain install stable --profile minimal --component clippy,rustfmtexport RUSTUP_DIST_SERVER=https://<your-mirror>/dist
export RUSTUP_UPDATE_ROOT=https://<your-mirror>/update
rustup update
[source.crates-io]
replace-with = "aliyun"
[source.aliyun]
registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"
which cargo 应指向 ~/.cargo/bin/cargo;若系统包管理器也装了 cargo,确保其路径在后面。rustup component add clippy --toolchain nightly)。