在 Debian 上用 Rust 进行开发,一般流程包括:安装系统依赖 → 安装 Rust 工具链 → 创建/管理项目 → 编译运行/调试 → 常用生态工具。下面按步骤说明。
sudo apt update
sudo apt upgrade -y
Rust 编译某些 crate(如 openssl、sqlite)需要系统库和编译器:
sudo apt install -y \
build-essential \
pkg-config \
libssl-dev \
curl \
git \
ca-certificates
(可选,按需)
sudo apt install -y clang libclang-dev
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择 1) Default installation
安装完成后:
source "$HOME/.cargo/env"
验证:
rustc --version
cargo --version
cargo new hello_rust
cd hello_rust
目录结构:
hello_rust
├── Cargo.toml
└── src
└── main.rs
运行:
cargo run
编译(不运行):
cargo build
发布编译(优化):
cargo build --release
| 命令 | 作用 |
|---|---|
cargo new |
新建项目 |
cargo build |
编译 |
cargo run |
编译并运行 |
cargo test |
运行测试 |
cargo fmt |
格式化代码 |
cargo clippy |
静态检查 |
cargo add xxx |
添加依赖 |
cargo update |
更新依赖 |
安装 clippy / fmt:
rustup component add clippy rustfmt
sudo apt install -y code
安装插件:
rust-analyzerEven Better TOMLrust-analyzer + LSP// src/main.rs
fn main() {
loop {
println!("running...");
std::thread::sleep(std::time::Duration::from_secs(5));
}
}
部署为 systemd 服务(略)
[dependencies]
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rustup toolchain install stable
rustup toolchain install nightly
rustup target add armv7-unknown-linux-gnueabihf
cargo build --target armv7-unknown-linux-gnueabihf
error: linker cc not foundsudo apt install -y gcc
sudo apt install -y libssl-dev pkg-config
如果你愿意,我可以:
你打算用 Rust 做什么类型的项目?