在 Debian 上用 Rust 编译程序,通常分为 安装 Rust 工具链 → 编写代码 → 编译运行 几个步骤。下面给你一个清晰、常用的流程。
rustup(最推荐)curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装完成后,按提示执行:
source "$HOME/.cargo/env"
验证是否成功:
rustc --version
cargo --version
sudo apt update
sudo apt install rustc cargo
⚠️ 版本通常较旧,不建议用于新项目。
cargo new hello
cd hello
目录结构:
hello
├── Cargo.toml
└── src
└── main.rs
src/main.rs:
fn main() {
println!("Hello, Debian!");
}
cargo run
cargo build
生成文件:
target/debug/hello
运行:
./target/debug/hello
cargo build --release
./target/release/hello
如果编译失败,可能需要:
sudo apt install build-essential
部分项目(如 OpenSSL)还需要:
sudo apt install pkg-config libssl-dev
rustcsource "$HOME/.cargo/env"
或写入 .bashrc / .zshrc:
export PATH="$HOME/.cargo/bin:$PATH"
可以,但需额外 target:
rustup target add x86_64-unknown-linux-musl
如果你愿意,可以告诉我:
我可以给你更针对性的方案。