在 Ubuntu 上配置 Rust 开发环境,一般按下面步骤来即可,适用于 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
或者重新打开终端。
rustc --version
cargo --version
编辑配置文件:
mkdir -p ~/.cargo
nano ~/.cargo/config.toml
示例(USTC 镜像):
[source.crates-io]
replace-with = "ustc"
[source.ustc]
registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"
保存后退出。
cargo new hello
cd hello
cargo run
输出:
Hello, world!
rustup component add rustfmt
rustup component add clippy
使用:
cargo fmt
cargo clippy
sudo snap install code --classic
安装扩展:
rustup update
rustup self uninstall
如果你是用在 WSL / Docker / 服务器 / 嵌入式 环境,或者想装 特定 Rust 版本 / nightly,可以告诉我,我可以给你更具体的配置方案。