在Debian系統上使用Rust可以分為以下幾個步驟:
sudo apt update
sudo apt upgrade -y
sudo apt install curl build-essential gcc make -y
rustup
安裝Rust:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path -y
這個命令會下載并運行rustup
的安裝腳本,按照提示操作。通常,rustup
會自動將Rust的工具鏈添加到你的PATH
環境變量中。
為了確保環境變量永久生效,可以將以下內容添加到~/.bashrc
或~/.profile
文件中:
echo 'export PATH="$HOME/.cargo/bin:$PATH"' | sudo tee -a /etc/profile.d/rust.sh
source /etc/profile
安裝完成后,可以通過以下命令驗證Rust是否安裝成功:
rustc --version
cargo --version
如果顯示了版本號,說明Rust已經成功安裝。
cargo new hello_world
cd hello_world
src/main.rs
文件,并輸入以下代碼:fn main() {
println!("Hello, world!");
}
cargo run
這將編譯你的Rust代碼并運行生成的可執行文件。如果一切正常,你應該會在終端看到輸出:
Compiling hello_world v0.1.0 (/path/to/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 0.34s
Running `target/debug/hello_world`
Hello, world!
cargo build --release
這將在target/release
目錄下生成可執行文件,你可以直接運行這個文件:
./target/release/hello_world
以上步驟應該能夠幫助你在Debian系統上成功安裝和配置Rust編程語言,并搭建一個基本的Rust開發環境。