在 Linux 上使用 Rust,一般流程是:安装 Rust → 写代码 → 编译运行 → (可选)用 Cargo 管理项目。下面一步一步说。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装完成后,执行:
source "$HOME/.cargo/env"
或者重新打开终端。
验证是否安装成功:
rustc --version
cargo --version
例如 Ubuntu:
sudo apt install rustc cargo
mkdir ~/rust_demo
cd ~/rust_demo
nano hello.rs
hello.rs 内容:
fn main() {
println!("Hello, Linux + Rust!");
}
rustc hello.rs
./hello
Cargo 是 Rust 的构建和包管理工具。
cargo new hello_cargo
cd hello_cargo
目录结构:
hello_cargo
├── Cargo.toml
└── src
└── main.rs
cargo run
cargo build
发布版本:
cargo build --release
确认环境变量:
export PATH="$HOME/.cargo/bin:$PATH"
可写入 ~/.bashrc 或 ~/.zshrc。
sudo apt install build-essential
rustup update
rustup self uninstall
如果你是想:
可以告诉我你的目标,我可以给你更具体的 Rust 使用方案。