下面给你一份在 Ubuntu 上配置 Rust 开发环境的实用指南,从安装到常见配置,适合新手和日常开发。
rustup(最推荐)curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择:
安装完成后:
source "$HOME/.cargo/env"
验证:
rustc --version
cargo --version
如果新开终端找不到 cargo,在 ~/.bashrc 或 ~/.zshrc 中添加:
export PATH="$HOME/.cargo/bin:$PATH"
然后:
source ~/.bashrc
rustup update
查看已安装工具链:
rustup toolchain list
安装特定版本(例如 nightly):
rustup install nightly
rustup default nightly
rustup component add rustfmt clippy
rustfmt:代码格式化clippy:代码静态检查使用:
cargo fmt
cargo clippy
编辑或创建 ~/.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"
export RUSTUP_DIST_SERVER=https://rsproxy.cn
export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup
sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev
常见额外依赖:
libclang-dev(bindgen)cmake(一些 C 依赖)protobuf-compilercargo new hello_rust
cd hello_rust
cargo run
rust-analyzercargo-editcargo-watchcargo-nextest安装示例:
cargo install cargo-edit cargo-watch
不要使用 sudo 安装 rustup
sudo apt install ca-certificates
cargo build --releasesccache如果你愿意,可以告诉我:
我可以给你更针对性的配置方案。