文章目录
- 基础
- 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/