在 Linux 上运行 Rust 程序,一般分为 安装 Rust → 写代码 → 编译 → 运行 几个步骤。下面是最常见、最推荐的做法。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
按提示选择:
source $HOME/.cargo/env
rustc --version
cargo --version
能看到版本号说明安装成功。
mkdir ~/rustdemo
cd ~/rustdemo
nano hello.rs
写入:
fn main() {
println!("Hello, Linux Rust!");
}
编译并运行:
rustc hello.rs
./hello
cargo new hello_rust
cd hello_rust
目录结构:
hello_rust
├── Cargo.toml
└── src
└── main.rs
cargo run
Cargo 会自动:
src/main.rs| 功能 | 命令 |
|---|---|
| 编译 | cargo build |
| 编译(发布模式) | cargo build --release |
| 运行 | cargo run |
| 检查(不生成可执行文件) | cargo check |
| 更新工具链 | rustup update |
Rust 有时需要 gcc:
sudo apt install build-essential # Ubuntu / Debian
sudo dnf groupinstall "Development Tools" # Fedora
rustup default stable
rustup default nightly
如果你愿意,我可以:
你现在是刚入门,还是已经有具体目标了?