Ubuntu Rust 配置有哪些技巧
导读:Ubuntu 下 Rust 配置实用技巧 一 基础安装与环境准备 使用rustup安装最新稳定版(推荐):先安装系统依赖,再下载并运行安装脚本,最后激活环境并验证版本。示例: sudo apt update && sud...
Ubuntu 下 Rust 配置实用技巧
一 基础安装与环境准备
- 使用rustup安装最新稳定版(推荐):先安装系统依赖,再下载并运行安装脚本,最后激活环境并验证版本。示例:
- sudo apt update & & sudo apt install -y curl build-essential gcc make
- curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
- source $HOME/.cargo/env
- rustc --version & & cargo --version
- 若遇到网络慢,可在安装前设置UStC 镜像环境变量以加速工具链下载:
- export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
- export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
- 不建议长期使用系统包管理器(apt)自带的 rustc/cargo,版本通常较旧;优先使用 rustup 获取最新稳定版与完整工具链。
二 镜像与网络加速
- 工具链安装加速:设置环境变量后执行安装脚本,可明显提升下载速度(见上节)。
- Crates 索引与注册表镜像:在 $HOME/.cargo/config.toml 中配置国内镜像,解决依赖拉取慢或失败问题。示例(任选其一或组合):
- 清华大学:
- [registries.crates-io]
- index = “https://mirrors.tuna.tsinghua.edu.cn/crates.io-index”
- [registries.crates-io]
- 中国科学技术大学(git 协议,部分环境更稳定):
- [source.crates-io]
- replace-with = ‘ustc’
- [source.ustc]
- registry = “git://mirrors.ustc.edu.cn/crates.io-index”
- [source.crates-io]
- 清华大学:
- 提示:镜像偶尔会同步滞后或调整,出现解析失败时临时切回官方源或换镜像即可。
三 工具链与组件管理
- 多版本与通道管理:
- 查看:rustup toolchain list
- 安装:rustup toolchain install 1.75.0 / nightly
- 切换:rustup default stable | nightly
- 项目级锁定:在项目根目录创建 rust-toolchain.toml
- [toolchain]
- channel = “1.75.0”
- components = [“rustfmt”, “clippy”, “rust-src”]
- targets = [“x86_64-unknown-linux-gnu”, “wasm32-unknown-unknown”]
- [toolchain]
- 常用组件(按需安装):
- rustup component add rustfmt clippy rust-src
- 新版已可直接安装 rust-analyzer(作为 rustup 组件),无需单独 cargo install。
四 编辑器与调试配置
- VSCode 高效开发:
- 核心插件:安装 rust-lang.rust-analyzer(官方 LSP)、vadimcn.vscode-lldb(调试)。
- 保存即检查与格式化:
- “editor.formatOnSave”: true
- “[rust]”: { “editor.defaultFormatter”: “rust-lang.rust-analyzer” }
- “rust-analyzer.checkOnSave.command”: “clippy”
- 大型项目性能优化(示例):
- “rust-analyzer.cargo.allFeatures”: false
- “rust-analyzer.checkOnSave.allTargets”: false
- “rust-analyzer.procMacro.enable”: true
- JetBrains 系列(IntelliJ IDEA/CLion):安装 Rust 插件,获得项目创建、构建、运行与调试的一体化支持。
五 常见问题与进阶场景
- 编译报错 linker ‘cc’ not found:安装 build-essential(或 gcc)以提供系统链接器与基础编译环境。
- 交叉编译与嵌入式开发:
- 查看目标:rustup target list
- 安装目标(示例):rustup target add riscv32imac-unknown-none-elf
- 调试器:准备 riscv-none-elf-gdb 或使用 gdb-multiarch(注意部分版本对 RISC-V 支持有限)。
- 项目级环境隔离:使用 rust-toolchain.toml 固定工具链版本与组件,避免团队成员环境不一致。
- 快速创建与运行项目:
- cargo new hello_world & & cd hello_world
- cargo build & & cargo run。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu Rust 配置有哪些技巧
本文地址: https://pptw.com/jishu/764332.html
