以下是在Ubuntu下配置Rust的步骤:
更新系统
sudo apt update && sudo apt upgrade
安装依赖库
sudo apt install curl build-essential gcc make
使用rustup安装Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择默认安装(安装最新稳定版Rust和Cargo)。
激活环境变量
source $HOME/.cargo/env
或重启终端。
验证安装
rustc --version # 查看Rust编译器版本
cargo --version # 查看Cargo版本
(可选)配置国内镜像源
编辑~/.cargo/config.toml,添加以下内容(以清华镜像为例):
[registries.crates-io]
index = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"
测试运行
创建并运行第一个程序:
cargo new hello_world
cd hello_world
cargo run
说明:推荐使用rustup安装,可灵活管理工具链版本;若需系统级安装,也可通过apt安装rustc和cargo,但版本可能较旧。