温馨提示×

Rust开发Debian怎配置

小樊
47
2026-09-29 07:54:34
栏目: 编程语言

下面按在 Debian 上用 Rust 进行开发这个场景,给你一个从系统配置到常用工具的一站式说明。


一、系统准备(Debian)

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装基础编译工具

Rust 需要 C 链接器和 make 等:

sudo apt install -y build-essential pkg-config libssl-dev

常用额外依赖(视项目而定):

sudo apt install -y cmake git curl

二、安装 Rust(推荐方式)

✅ 使用 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

三、配置环境变量(如未自动生效)

编辑 ~/.bashrc 或 ~/.zshrc:

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

生效:

source ~/.bashrc

四、Cargo 国内镜像(可选,提速)

编辑 ~/.cargo/config.toml:

[source.crates-io]
replace-with = "rsproxy"

[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"

[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"

或使用清华镜像:

[source.crates-io]
replace-with = "tuna"

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"

五、创建一个 Rust 项目

cargo new hello
cd hello
cargo run

六、常用开发工具(强烈推荐)

1. 代码补全 / LSP

rustup component add rust-analyzer

或在编辑器安装 rust-analyzer

2. 代码格式化 & 检查

rustup component add rustfmt clippy

使用:

cargo fmt
cargo clippy

七、Debian 上常见问题

1. 缺少 OpenSSL

报错:openssl not found

sudo apt install -y libssl-dev pkg-config

2. 静态编译(服务器常用)

sudo apt install -y musl-tools
rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl

八、编辑器推荐

  • VS Code + rust-analyzer
  • Neovim + rust-tools / lspconfig
  • Helix(hx 原生支持 Rust)

九、系统服务(可选)

如果是写后端服务,可用:

  • systemd
  • cargo install cargo-deb(打包成 .deb)
cargo install cargo-deb
cargo deb

如果你有具体用途(比如 Web 服务 / 系统程序 / 嵌入式 / GUI),我可以给你更针对性的 Debian + Rust 配置方案。

0 踩