Ubuntu 下 Rust 配置步骤
一 准备与安装
sudo apt update && sudo apt install -y curl build-essential gcc makecurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shsource "$HOME/.cargo/env"(或新开一个终端会话)。rustc --version、cargo --version、rustup --version,应显示版本号与路径 $HOME/.cargo/bin。二 国内镜像加速(可选)
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-staticexport RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup$HOME/.cargo/config.toml[registries]
crates-io = { index = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index" }
三 基础配置与管理
rustup toolchain listrustup toolchain install nightlyrustup default nightlyrustup run nightly cargo buildrustfmt(格式化)、clippy(代码检查)、rust-docs(文档)rustup show profile,可选 minimal、default、completerustup component add clippy rustfmtrustup updaterustup self uninstall四 创建项目与构建运行
cargo new hello_world && cd hello_worldcargo buildcargo runcargo build --releaserustc main.rs && ./main五 常见问题与排查
sudo apt install -y build-essentialrustc、cargo):
source "$HOME/.cargo/env",或确认 echo "$PATH" 包含 $HOME/.cargo/bin