温馨提示×

Rust在Debian怎么编译

小樊
41
2026-10-14 00:07:16
栏目: 编程语言

在 Debian 上编译 Rust 程序,通常分为安装 Rust 工具链和编译运行两部分。下面给你一份清晰、常用的步骤。


一、安装 Rust(推荐方式)

方式 1:使用官方 rustup(最推荐)

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

按提示选择 1) Proceed with installation (default)
安装完成后,加载环境:

source "$HOME/.cargo/env"

验证:

rustc --version
cargo --version

方式 2:Debian 官方仓库(版本较旧)

sudo apt update
sudo apt install rustc cargo

⚠️ 不推荐用于新项目,版本通常滞后。


二、编译 Rust 程序

1️⃣ 编译单个文件

rustc main.rs
./main

2️⃣ 使用 Cargo(推荐)

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" 或重新登录
  • 网络问题:可配置镜像(如 USTC / TUNA)

如果你是 交叉编译 / 静态编译 / 服务器部署,可以告诉我场景,我可以给你更具体的方案。

0 踩