下面按从零开始的思路,给你一份 Linux 下 Rust 开发环境配置的实用指南。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装完成后,按提示执行:
source "$HOME/.cargo/env"
或重新打开终端。
rustc --version
cargo --version
看到版本号说明安装成功。
rustup update
rustup show
常用组件:
rustup component add rustfmt # 代码格式化
rustup component add clippy # lint 工具
cargo new hello_rust
cd hello_rust
cargo run
项目结构:
hello_rust/
├── Cargo.toml
└── src
└── main.rs
# Debian / Ubuntu
sudo apt install build-essential pkg-config libssl-dev
# Fedora
sudo dnf install gcc openssl-devel
# Arch
sudo pacman -S base-devel openssl
openssllibgtk-3-dev-dev 包code .
安装扩展:
rust-analyzer(必装)Even Better TOMLrust-analyzer + LSP编辑 ~/.cargo/config.toml:
[source.crates-io]
replace-with = "rsproxy"
[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"
或:
[source.crates-io]
replace-with = "tuna"
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"
rustup doc --book
或在线:https://doc.rust-lang.org/book
如果你愿意,可以告诉我:
我可以直接给你一套更精准的配置方案。