Debian Rust配置步骤是什么
小樊
39
2025-12-24 21:24:02
Debian 上配置 Rust 的推荐步骤
一 准备环境
更新索引并安装常用构建工具与 curl (用于下载安装脚本):
sudo apt update
sudo apt install -y curl build-essential gcc make
这些依赖可确保后续通过源码构建 crate 时具备必要的编译与链接工具。
二 安装方式选择
方式 A(推荐)使用官方版本管理器 rustup
安装命令:curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
使环境变量生效:source “$HOME/.cargo/env”
验证:rustc --version 与 cargo --version 均应输出版本号
说明:rustup 便于在 stable / beta / nightly 间切换、按需安装组件与跨编译目标。
方式 B 使用发行版仓库安装稳定版
安装命令:sudo apt install -y rustc cargo
验证:rustc --version 与 cargo --version
说明:版本通常较旧,但稳定、集成系统包管理;如需最新特性与工具链管理,仍建议 rustup。
三 配置工具链与常用组件
设置默认工具链(示例设为稳定版):rustup default stable
按需添加常用组件:rustup component add rust-src rust-analysis rustfmt clippy
以上可提升开发体验(格式化、静态检查、源码访问等)。
四 创建项目与运行
使用 Cargo 快速开始:
新建项目:cargo new hello_rust && cd hello_rust
运行:cargo run
构建发布版:cargo build --release
以上命令会在项目目录生成可执行文件,便于开发与发布。
五 环境变量与多用户或自定义安装
用户级安装(默认):确保 $HOME/.cargo/bin 在 PATH ,可在 ~/.bashrc / ~/.zshrc 中添加:export PATH=“$HOME/.cargo/bin:$PATH”,并执行 source 相应文件
全局安装(系统多用户场景,示例安装到 /opt/rust ):
安装:wget -qO - https://sh.rustup.rs | sudo RUSTUP_HOME=/opt/rust CARGO_HOME=/opt/rust sh -s – --no-modify-path -y
全局生效:echo ‘export RUSTUP_HOME=/opt/rust’ | sudo tee -a /etc/profile.d/rust.sh;echo ‘export PATH=$PATH:/opt/rust/bin’ | sudo tee -a /etc/profile.d/rust.sh
重新加载:source /etc/profile
验证:echo $RUSTUP_HOME 与 rustc --version。