温馨提示×

如何在Debian中查找Rust资源

小樊
37
2026-01-07 12:48:21
栏目: 编程语言

在 Debian 中查找与获取 Rust 资源的实用指南

一 安装与获取 Rust 资源

  • 使用发行版仓库安装(稳定、系统级):更新索引后安装 cargo(会自动拉取 rustc),适合希望随系统统一更新的场景。命令:sudo apt update && sudo apt install cargo;验证:cargo --versionrustc --version
  • 使用官方 rustup(最新、多版本管理):无需 root,安装到用户目录,便于切换 stable/beta/nightly 和组件。命令:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh,完成后执行 source "$HOME/.cargo/env",验证:rustc --versioncargo --version。如需更新:rustup update;如需卸载:rustup self uninstall
  • 构建依赖:首次从源码构建常见 Rust 项目时,建议安装基础构建工具,例如 sudo apt install build-essential

二 查找与安装 Rust 软件包与库

  • 发行版仓库查找:用 apt search rust- 或访问 Debian Packages 网站搜索含有关键字 rust- 的包,适合查找系统提供的库/工具(版本通常较稳)。
  • 通过 Cargo 查找与获取:在项目中编辑 Cargo.toml,添加依赖如 rand = "0.8";运行 cargo build 自动从 crates.io 下载并编译依赖。示例:
    [dependencies]
    rand = "0.8"
    
    之后使用 cargo buildcargo run 完成构建与运行。

三 配置国内镜像加速

  • 加速工具链安装:设置环境变量使用国内镜像(如 USTC)提升下载速度:
    export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
    export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
    
  • 加速 crate 索引与下载:在用户目录创建或编辑 ~/.cargo/config,使用 rsproxy 镜像:
    [source.crates-io]
    replace-with = 'rsproxy'
    
    [source.rsproxy]
    registry = "https://rsproxy.cn/crates.io-index"
    
    [registries.rsproxy]
    index = "https://rsproxy.cn/crates.io-index"
    
    [net]
    git-fetch-with-cli = true
    
    说明:上述镜像仅加速包索引与下载,不影响工具链管理(rustup)。

四 常用查找与管理命令

  • 版本与工具链:rustc --versioncargo --versionrustup show(查看当前工具链与组件)。
  • 更新与卸载:工具链更新用 rustup update;卸载官方工具链用 rustup self uninstall;卸载发行版包用 sudo apt remove cargosudo apt autoremove
  • 构建与运行:在项目根目录执行 cargo build(构建)、cargo run(构建并运行)、cargo test(运行测试)。

0