下面按「从零开始」的思路,给你一份在 Ubuntu 上配置 Rust 开发环境 的实用指南,适用于 Ubuntu 20.04 / 22.04 / 24.04。
不要直接用 apt 安装(版本旧),用官方工具链:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装过程中选择:
1) Proceed with standard installation(默认即可)完成后:
source "$HOME/.cargo/env"
或重新打开终端。
验证:
rustc --version
cargo --version
确认 ~/.bashrc 或 ~/.zshrc 中有:
export PATH="$HOME/.cargo/bin:$PATH"
如果用了 zsh:
source ~/.zshrc
mkdir -p ~/.cargo
编辑 ~/.cargo/config.toml:
[source.crates-io]
replace-with = "rsproxy"
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"
或使用清华源:
[source.crates-io]
replace-with = "tuna"
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
rustup component add rustfmt
rustup component add clippy
使用:
cargo fmt
cargo clippy
cargo install cargo-watch
cargo install cargo-udeps
如果你要写系统级 / GUI / 网络程序,先装:
sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev
常见额外依赖:
libgtk-3-devlibsqlite3-devlibasound2-dev安装插件:
设置中启用:
"rust-analyzer.check.command": "clippy"
cargo new hello
cd hello
cargo run
升级:
rustup update
卸载:
rustup self uninstall
error: linker cc not foundsudo apt install build-essential
rustup target add x86_64-pc-windows-gnu
如果你告诉我:
我可以给你更针对性的配置方案。