文章目錄
- 基礎
- references
基礎
- hello,world是幾乎所有編程語言的第一例子,rust也不例外。但和其它語言不一樣,Rust的源碼最好擁有自己的項目目錄。
$ mkdir ~/pro
$ cd ~/pro
$ mkdir helloWorld
$ cd helloWorld
源代碼文件名為main.rs,內容如下
fn main() {println!("你好,世界!");
}
$ rustc main.rs
$ ./main
你好,世界!
- 作為rust程序員,cargo是必須熟悉的工具組,它是包管理器和編譯系統,尤其是在編程任務量大的情況下,它能有效提高工作效率。
雖然hello,world很簡單,但為了養成良好習慣,我們也可以使用cargo來管理這個項目。
PS E:\learn\rust> cargo new helloCreating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
PS E:\learn\rust> cd hello
PS E:\learn\rust\hello>
hello目錄下有兩個文件和一個目錄,這個名為src的目錄下唯一的源代碼文件main.rs,這是cargo默認為我們創建好的。
PS E:\learn\rust\hello> lsDirectory: E:\learn\rust\helloMode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2025/9/14 7:41 src
-a--- 2025/9/14 7:41 8 .gitignore
-a--- 2025/9/14 7:41 76 Cargo.tomlPS E:\learn\rust\hello>
PS E:\learn\rust\hello> ls srcDirectory: E:\learn\rust\hello\srcMode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2025/9/14 7:41 45 main.rs
- “ciao,mondo” è il primo esempio in quasi tutti linguaggi di programmazione,Rust non fa eccezione.
references
- https://github.com/rust-lang/book/