下面给你一份在 Ubuntu 上安装并配置 Rust 的标准做法(2024 年仍适用,适用于 Ubuntu 20.04 / 22.04 / 24.04)。
sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential curl git
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装过程中会让你选择:
安装完成后执行:
source "$HOME/.cargo/env"
✅ 建议把下面这行加进 ~/.bashrc(或 ~/.zshrc):
export PATH="$HOME/.cargo/bin:$PATH"
然后:
source ~/.bashrc
rustc --version
cargo --version
看到类似输出说明成功:
rustc 1.80.1 (...)
cargo 1.80.1 (...)
rustup default stable
rustup component add rustfmt clippy
rustfmt:代码格式化clippy:代码静态检查编辑 ~/.cargo/config.toml:
[source.crates-io]
replace-with = 'rsproxy'
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[net]
git-fetch-with-cli = true
cargo new hello
cd hello
cargo run
Rust 是用户级安装,不要用 sudo cargo
rustup self uninstall
如果你是要:
可以继续告诉我你的使用场景,我可以给你更针对性的配置。