在 Debian 上运行 Rust 程序,一般分几种情况:直接运行已有的 Rust 程序(二进制)、运行 Rust 源码项目、以及从零开始写并运行。下面按常见场景说明。
如果你已经有一个编译好的 Rust 可执行文件:
./your_program
如果提示权限不足:
chmod +x your_program
./your_program
推荐用官方方式(最通用):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示完成后:
source "$HOME/.cargo/env"
验证:
rustc --version
cargo --version
也可以直接用 Debian 源安装(版本可能偏旧):
sudo apt update sudo apt install rustc cargo
假设项目目录里有 Cargo.toml:
cd your_project
cargo run
只编译不运行:
cargo build
./target/debug/your_project
发布模式(更快):
cargo build --release
./target/release/your_project
mkdir hello && cd hello
nano main.rs
main.rs 内容:
fn main() {
println!("Hello, Debian!");
}
rustc main.rs
./main
输出:
Hello, Debian!
运行某些 Rust 程序可能报错:
error while loading shared libraries: libssl.so.1.1
解决:
sudo apt install libssl-dev pkg-config
建议使用 rustup 而不是 apt:
rustup update
如果需要开机自启,可用 systemd:
[Unit]
Description=Rust App
[Service]
ExecStart=/path/to/your_program
WorkingDirectory=/path/to/
Restart=always
[Install]
WantedBy=multi-user.target
然后:
sudo systemctl enable yourapp
sudo systemctl start yourapp
如果你能告诉我:
我可以给你更精确的步骤。