以下是在Linux上完美配置Rust环境的步骤,涵盖安装、环境变量配置及常用工具设置:
通过官方脚本安装,支持多版本切换:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择默认安装(包含rustc编译器、cargo包管理器等)。
安装后需将Rust工具链路径加入PATH:
~/.bashrc(或~/.zshrc):export PATH="$HOME/.cargo/bin:$PATH"
source ~/.bashrc
验证安装:
rustc --version # 查看Rust编译器版本
cargo --version # 查看包管理器版本
修改~/.cargo/config文件,添加国内镜像(如中科大或清华源):
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"
需先安装curl和build-essential等依赖。
rustup component add rustfmtrustup component add clippyrustup component add rustdoccargo test(无需额外安装)创建一个简单项目测试:
cargo new hello-world
cd hello-world
cargo run # 编译并运行,输出"Hello, world!"
若能正常输出,说明环境配置成功。
bash shell(如zsh),需对应修改配置文件(如~/.zshrc)。rustup default <版本号>或rustup override set <版本号>。以上步骤参考自官方文档及社区实践,可确保Rust环境在Linux系统上稳定运行。