温馨提示×

怎样在Debian用Rust开发

小樊
48
2026-10-02 05:24:43
栏目: 编程语言

在 Debian 上用 Rust 进行开发,一般流程包括:安装系统依赖 → 安装 Rust 工具链 → 创建/管理项目 → 编译运行/调试 → 常用生态工具。下面按步骤说明。


一、准备 Debian 系统环境

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装基础编译依赖

Rust 编译某些 crate(如 openssl、sqlite)需要系统库和编译器:

sudo apt install -y \
  build-essential \
  pkg-config \
  libssl-dev \
  curl \
  git \
  ca-certificates

(可选,按需)

sudo apt install -y clang libclang-dev

二、安装 Rust 工具链(推荐方式)

✅ 使用 rustup(官方推荐)

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

按提示选择 1) Default installation

安装完成后:

source "$HOME/.cargo/env"

验证:

rustc --version
cargo --version

三、创建第一个 Rust 项目

cargo new hello_rust
cd hello_rust

目录结构:

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

运行:

cargo run

编译(不运行):

cargo build

发布编译(优化):

cargo build --release

四、常用 Cargo 命令

命令 作用
cargo new 新建项目
cargo build 编译
cargo run 编译并运行
cargo test 运行测试
cargo fmt 格式化代码
cargo clippy 静态检查
cargo add xxx 添加依赖
cargo update 更新依赖

安装 clippy / fmt:

rustup component add clippy rustfmt

五、开发环境(编辑器 / IDE)

推荐:VS Code

sudo apt install -y code

安装插件:

  • rust-analyzer
  • Even Better TOML

或 NeoVim / Vim

  • rust-analyzer + LSP

六、系统级 / 嵌入式 / 服务开发示例

1. 写系统服务(systemd)

// src/main.rs
fn main() {
    loop {
        println!("running...");
        std::thread::sleep(std::time::Duration::from_secs(5));
    }
}

部署为 systemd 服务(略)


2. 使用常见库

[dependencies]
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

七、多版本 / 交叉编译(进阶)

安装其他工具链

rustup toolchain install stable
rustup toolchain install nightly

交叉编译(如 ARM)

rustup target add armv7-unknown-linux-gnueabihf
cargo build --target armv7-unknown-linux-gnueabihf

八、常见问题

❌ error: linker cc not found

sudo apt install -y gcc

❌ SSL 相关错误

sudo apt install -y libssl-dev pkg-config

如果你愿意,我可以:

  • 给你一个 Debian + Rust Web 服务示例
  • 讲 Rust + PostgreSQL / Redis
  • 或帮你配置 Rust 开发容器(Docker)

你打算用 Rust 做什么类型的项目?

0 踩