溫馨提示×

Rust在Debian上怎么用

小樊
46
2025-07-16 06:01:06
欄目: 編程語言

在Debian系統上使用Rust可以分為以下幾個步驟:

安裝Rust

  1. 更新系統包
sudo apt update
sudo apt upgrade -y
  1. 安裝必要的構建工具
sudo apt install curl build-essential gcc make -y
  1. 使用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管理Rust項目

  1. 創建一個新的Rust項目
cargo new hello_world
cd hello_world
  1. 編寫Rust代碼: 使用文本編輯器打開src/main.rs文件,并輸入以下代碼:
fn main() {
    println!("Hello, world!");
}
  1. 編譯并運行Rust程序
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!
  1. 構建發布版本(可選): 如果你想要構建一個優化的發布版本,可以使用以下命令:
cargo build --release

這將在target/release目錄下生成可執行文件,你可以直接運行這個文件:

./target/release/hello_world

以上步驟應該能夠幫助你在Debian系統上成功安裝和配置Rust編程語言,并搭建一個基本的Rust開發環境。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女