Rust程式語言入門
開始動手體驗Rust的第一步是安裝Rust。為了安裝Rust,我們需要一個Rust安裝程式。
Rustup是一個版本管理工具,也是一個安裝程式,它可以幫助你在本地機器上安裝Rust。
如果你執行的是Linux、macOS或其他類Unix作業系統,那麼我們只需要在終端執行以下命令:
curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh
以上命令將在你的本地機器上安裝Rust。
如果你是Windows使用者,則可以從此連結下載.exe檔案:rustup-init.exe
保持Rust更新
雖然Rust會頻繁更新,但你可以始終透過執行以下命令來確保它是最新版本:
rustup update
Cargo:包管理器
- 安裝Rustup後,你還會獲得Rust最新版本的包管理器和構建工具,也稱為Cargo。Cargo對於Rust至關重要,它允許我們執行以下操作:
- 使用`cargo build`構建專案
- 使用`cargo run`執行專案
- 使用`cargo test`測試專案
- 使用`cargo doc`為專案構建文件
我們可以透過在我們選擇的任何終端中執行以下命令來測試我們是否已正確安裝Rust和Cargo:
cargo --version
建立新專案
讓我們使用Cargo建立一個新的Rust專案。在你的終端中,執行以下命令:
cargo new hello-tutorialspoint
以上命令將生成一個名為hello-tutorialspoint的新目錄,其中包含以下檔案:
hello-tutorialspoint |-- Cargo.toml |-- src |-- main.rs
這裡:
- Cargo.toml - Rust的清單檔案。
- src/main.rs - 將編寫你的應用程式程式碼的地方。
每當學習一門新的程式語言時,編寫“Hello, world!”程式都是一種傳統。在Rust中,Cargo為我們提供了同樣的功能,我們只需要在終端執行以下命令:
cargo new
以上命令將生成一個“Hello, world!”專案。為了執行該專案,我們需要進入我們建立的新目錄,並在終端執行以下命令:
cargo run
執行此命令後,你將看到類似以下內容:
Compiling hello-tutorialspoint v0.1.0 (/Users/immukul/hello-tutorialspoint) Finished dev [unoptimized + debuginfo] target(s) in 2.39s Running `target/debug/hello-tutorialspoint` Hello, world!
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP