在Debian上使用Rust編譯器需先安裝Rust工具鏈,再通過rustc或cargo命令操作,具體步驟如下:
安裝Rust工具鏈
rustup(官方工具鏈管理器):curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env # 配置環境變量
rustc --version # 查看編譯器版本
cargo --version # 查看包管理器版本
使用Rust編譯器
rustc hello.rs # 生成可執行文件hello
./hello # 運行程序
cargo new my_project # 生成項目目錄及默認代碼
cd my_project
cargo build # 調試模式編譯,生成target/debug/可執行文件
cargo run # 編譯并直接運行
cargo build --release # 優化模式編譯,生成target/release/可執行文件
可選:配置工具鏈版本
rustup show
rustup default nightly
說明:rustc是底層編譯器,cargo是高層構建工具,多數場景下通過cargo管理項目更便捷。安裝時需確保系統已更新依賴包(如curl、build-essential)。