温馨提示×

怎样在debian跑rust程序

小樊
44
2026-09-08 05:05:19
栏目: 编程语言

在 Debian 上运行 Rust 程序,一般分几种情况:直接运行已有的 Rust 程序(二进制)运行 Rust 源码项目、以及从零开始写并运行。下面按常见场景说明。


一、直接运行已编译好的 Rust 程序(.elf / 无后缀)

如果你已经有一个编译好的 Rust 可执行文件:

./your_program

如果提示权限不足:

chmod +x your_program
./your_program

二、在 Debian 上运行 Rust 源码项目

1. 安装 Rust 工具链

推荐用官方方式(最通用):

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

2. 运行已有 Rust 项目

假设项目目录里有 Cargo.toml

cd your_project
cargo run

只编译不运行:

cargo build
./target/debug/your_project

发布模式(更快):

cargo build --release
./target/release/your_project

三、从零写一个简单的 Rust 程序并运行

1. 写代码

mkdir hello && cd hello
nano main.rs

main.rs 内容:

fn main() {
    println!("Hello, Debian!");
}

2. 编译并运行

rustc main.rs
./main

输出:

Hello, Debian!

四、常见问题

1. 缺少依赖(如 OpenSSL)

运行某些 Rust 程序可能报错:

error while loading shared libraries: libssl.so.1.1

解决:

sudo apt install libssl-dev pkg-config

2. Debian 太老,Rust 版本低

建议使用 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

如果你能告诉我:

  • 运行别人编译好的程序 还是 自己写 Rust
  • Debian 版本(如 11 / 12)
  • 是否有 GUI 或纯服务器

我可以给你更精确的步骤。

0