在本文中,使用Rust語言編寫簡單的程式,以瞭解如何編寫,保存和編譯Rust程式。現在,打開記事本檔並編寫以下代碼:
fn main(){
println!("Hello, world!");
}
將上面內容保存到一個檔:hello.rs 中,然後使用 rustc hello.rs
命令來編譯運行上面程式,得到以下結果 -
Hello, world!
main()
:main()
函數始終是每個Rust可執行代碼中的第一個代碼。main()
函數用大括弧{}
括起來。main()
函數不包含任何參數,也不返回任何值。println!
:這是一個Rust宏。 如果它調用該函數,則它不包含符號:'!'
。"Hello World"
:它是作為參數傳遞給println!
的字串,字串將列印到控制臺。
創建,編譯和運行程式的過程
- 打開記事本檔並將代碼寫入記事本檔中。
- 使用
.rs
擴展名保存檔。 - 打開命令提示符
- 設置目錄的路徑,假設專案位於
/home/hema/worsp/rust
目錄中。 - 使用
rustc
命令編譯上述程式。 - 最後,使用命令
./filename
運行程式。
hema@zaixian:~/worsp/rust$ rustc hello.rs && ./hello
Hello World!
注:如果有遇到 “error: could not exec the linker
link.exe
: “ 之類的錯誤,請下載 Build Tools for Visual Studio 2017 (URL => https://visualstudio.microsoft.com/downloads/#title-58852 )
上一篇:
Rust開發環境安裝
下一篇:
Rust if語句