下面给你一份在 Ubuntu 上配置 Rust 开发环境的完整步骤,从零开始,适合新手。
sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential curl git
build-essential:提供 gcc、make 等编译工具curl:用于下载 Rust 安装脚本git:版本管理(常用)curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装过程中:
1) Proceed with installation (default)source "$HOME/.cargo/env"
(以后每次新开终端会自动加载)
rustc --version
cargo --version
看到类似输出说明成功:
rustc 1.78.0 (xxxxxx)
cargo 1.78.0 (xxxxxx)
nano ~/.cargo/config.toml
写入(以清华镜像为例):
[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"
保存退出。
cargo new hello_rust
cd hello_rust
cargo run
输出:
Hello, world!
| 命令 | 作用 |
|---|---|
cargo new 项目名 |
新建项目 |
cargo build |
编译 |
cargo run |
编译并运行 |
cargo check |
只检查不编译 |
rustup update |
升级 Rust |
sudo snap install code --classic
安装插件:
如果你需要:
告诉我你的使用场景,我可以给你更针对性的配置方案。