在 Debian 上编译 Rust 程序,通常分为安装 Rust 工具链和编译运行两部分。下面给你一份清晰、常用的步骤。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择 1) Proceed with installation (default)
安装完成后,加载环境:
source "$HOME/.cargo/env"
验证:
rustc --version
cargo --version
sudo apt update
sudo apt install rustc cargo
⚠️ 不推荐用于新项目,版本通常滞后。
rustc main.rs
./main
cargo new hello
cd hello
cargo build
调试运行:
cargo run
发布编译(优化):
cargo build --release
./target/release/hello
某些项目(如 OpenSSL、SQLite)需要:
sudo apt install build-essential pkg-config libssl-dev
source "$HOME/.cargo/env" 或重新登录如果你是 交叉编译 / 静态编译 / 服务器部署,可以告诉我场景,我可以给你更具体的方案。