温馨提示×

怎样在debian跑rust程

小樊
43
2026-09-26 10:04:21
栏目: 编程语言

在 Debian 上运行 Rust 程序一般有几种情况:直接运行 Rust 源码(编译后运行)、运行已经编译好的二进制文件、或者用 Cargo 管理项目。下面按常见步骤说明。


一、在 Debian 上安装 Rust 环境

方法 1:官方推荐(rustup,最常用)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

按提示完成后,加载环境:

source "$HOME/.cargo/env"

验证:

rustc --version
cargo --version

方法 2:Debian 软件源(版本可能较旧)

sudo apt update
sudo apt install rustc cargo

二、运行一个 Rust 程序(从源码)

1️⃣ 写一个简单的 Rust 程序

创建文件 main.rs:

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

2️⃣ 编译并运行

rustc main.rs
./main

输出:

Hello, Debian!

三、使用 Cargo(推荐方式)

1️⃣ 新建项目

cargo new hello
cd hello

目录结构:

hello
├── Cargo.toml
└── src
    └── main.rs

2️⃣ 编译并运行

cargo run

或者只编译:

cargo build
./target/debug/hello

发布版本(更快更小):

cargo build --release
./target/release/hello

四、运行别人编译好的 Rust 程序

如果是 Linux 二进制文件:

chmod +x program
./program

若提示缺少库(如 libssl、glibc):

sudo apt install libssl-dev

五、常见问题

❌ 命令找不到 rustc / cargo

source "$HOME/.cargo/env"

或把下面加到 ~/.bashrc:

export PATH="$HOME/.cargo/bin:$PATH"

❌ Debian 太老,glibc 不兼容

  • 用 静态编译
cargo build --release --target x86_64-unknown-linux-musl

如果你愿意,可以告诉我:

  • 是 桌面 Debian 还是服务器
  • 想跑 自己的程序还是现成项目
  • 是否要 开机自启 / systemd

我可以给你更具体的方案。

0 踩