温馨提示×

如何在Linux上安装Rust开发环境

小樊
39
2025-12-06 10:54:32
栏目: 编程语言

在 Linux 上安装 Rust 开发环境

一 安装步骤

  • 安装系统构建依赖(常见发行版)
    • Ubuntu/Debian:sudo apt update && sudo apt install -y curl build-essential gcc make
    • Fedora/RHEL/CentOS:sudo dnf groupinstall -y “Development Tools” && sudo dnf install -y curl
    • openSUSE:sudo zypper install -y curl gcc make
  • 下载并安装 rustup(Rust 工具链管理器):curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • 配置环境变量:source $HOME/.cargo/env
  • 验证安装:rustc --version 与 cargo --version 均应输出版本号

二 国内镜像加速(可选)

  • 设置镜像后再安装或更新:
    • export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
    • export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
    • 再执行安装脚本:curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • 说明:上述为安装阶段的镜像,能显著加快在国内的下载速度。

三 创建项目与运行

  • 创建新项目:cargo new hello-world && cd hello-world
  • 运行项目:cargo run(应看到输出:Hello, world!
  • 常用命令
    • 构建:cargo build
    • 运行:cargo run
    • 测试:cargo test
    • 格式化:cargo fmt
    • 静态检查:cargo clippy

四 编辑器与常用工具

  • VS Code 安装扩展:搜索并安装 rust-analyzer(提供补全、跳转、诊断等)
  • 其他常用工具
    • 代码格式化:rustfmt
    • 代码检查:clippy
    • 文档生成:cargo doc
    • 依赖管理增强:cargo-edit(如 cargo add/rm/upgrade)

五 版本管理与更新卸载

  • 查看与更新
    • 查看工具链:rustup show
    • 更新工具链:rustup update
  • 安装/切换工具链
    • 安装指定版本:rustup install 1.85.0
    • 设为默认:rustup default 1.85.0
    • 临时使用:rustup run 1.85.0 cargo build
  • 组件管理
    • 列出组件:rustup component list
    • 添加组件:rustup component add rust-src
  • 卸载
    • 卸载 Rust:rustup self uninstall
  • 本地文档
    • 打开离线文档:rustup doc

0