Rust 源代码组织,使用配套的 Cargo 工具,其功能强大,程序员可摆脱 C/C++ 中需要自行维护 make、cmake 之类配置的工作量。
初始化一个项目:
cargo new --bin hello_world
指定 --bin 选项代表创建的是一个直接可执行的二进制项目,否则会生成一个库项目。
执行 cargo run && cargo run --release 之后,项目目录结构如下:
zsh/3 114 (git)-[master]-%tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── debug
│ ├── build
│ ├── deps
│ │ └── hello_world-f745b285e01df5ca
│ ├── examples
│ ├── hello_world
│ ├── hello_world.d
│ ├── incremental
│ └── native
└── release
├── build
├── deps
│ └── hello_world-7399f171987fdf9d
├── examples
├── hello_world
├── hello_world.d
├── incremental
└── native
生成的二进制文件位于项目路径下的 target/debug 或 target/release 子目录中,--release 指生成编译优化版的二进制文件,类似于 C 语言开启 -O2 优化选项。
其中 Cargo.toml 是 Cargo 用来管理项目结构的配置文件,其初始内容如下:
zsh/3 116 (git)-[master]-% catCargo.toml
[package]
name= "hello_world"version= "0.1.0"authors= ["kt "]
[dependencies]
____
注:rust 生成的最终可执行文件,都是无外部依赖的静态编译结果。