1.2 第一个Rust程序
1.2.1 创建第一个 Rust 程序
Rust 社区推荐使用 Cargo 来管理 Rust 项目,因为它简化了许多常见的任务,比如编译代码、运行测试和发布包。
打开终端或命令提示符,并创建一个新的 Cargo 项目:
cargo new hello_world
使用 Cargo 创建一个新的 Rust 项目不仅快速简便,而且自动生成了一个合理的项目结构,这对于管理和扩展你的项目非常有帮助。
当你运行 cargo new hello_world
后,会得到如下目录结构:
hello_world/
├── Cargo.toml
└── src
└── main.rs
1.2.2 文件结构及作用解析
-
Cargo.toml
这是你的项目配置文件,也是 Cargo 用来了解如何构建你的项目的依据。它包含了项目的元数据(如名称、版本、作者等)、依赖项以及构建设置。一个基本的
Cargo.toml
文件可能看起来像这样:[package] name = "hello_world" version = "0.1.0" edition = "2021" [dependencies]
在这里&#x