每个 Rust 项目都从 cargo new 开始。这篇不讲命令表,而是真的跑一遍:cargo new 到底生成了哪些文件,第一次编译做了什么、再编译一次为什么几乎不花时间,target/ 里有什么,debug 与 release 的差别,以及 cargo check 为什么更快。
$ cargo new hello
Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
目录树:
.git/ (git 仓库)
.gitignore (8 B)
Cargo.toml (76 B)
src/
main.rs (45 B)
Cargo.toml:
[package]
name = "hello"
version = "0.1.0"
edition = "2024"
[dependencies]
src/main.rs:
fn main() {
println!("Hello, world!");
}
.gitignore: /targetedition 决定语言版本的一组规则(这里的 2024 是 rustc 1.98 新建项目的默认值);Cargo.toml 是人写的清单,Cargo.lock 是 cargo 生成的依赖锁定(见Cargo.lock 的作用)。
$ cargo build (第 1 次,0.23 s)
Compiling hello v0.1.0 (<实验目录>/proj/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.21s
$ cargo build (什么都没改,0.02 s)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
$ cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `<实验目录>/target/debug/hello`
Hello, world!
Cargo.lock(此刻出现):
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "hello"
version = "0.1.0"cargo 把「是否需要重编」交给指纹(target/debug/.fingerprint/)判断:源文件、依赖、编译参数都没变,就直接 Finished。
target/ 的结构(两层):
debug/.cargo-artifact-lock (0 B)
debug/.cargo-build-lock (0 B)
debug/.cargo-lock (0 B)
debug/.fingerprint/
debug/build/
debug/deps/
debug/examples/
debug/hello (438120 B)
debug/hello.d (279 B)
debug/incremental/
可执行文件 target/debug/hello:438120 字节(428 KiB)
改一行字符串后 $ cargo build(0.14 s):
Compiling hello v0.1.0 (<实验目录>/proj/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s这个目录可以随时删除,下一次 build 会重新生成;所以 .gitignore 里只有 /target。
$ cargo build --release(0.12 s)
Compiling hello v0.1.0 (<实验目录>/proj/hello)
Finished `release` profile [optimized] target(s) in 0.10s
release 可执行文件:433792 字节;debug:438120 字节
改源码后 $ cargo check(0.05 s):
Checking hello v0.1.0 (<实验目录>/proj/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
改源码后 $ cargo build(0.11 s)
$ target/release/hello 直接运行(不经过 cargo): Hello, rust! v2release 的优化效果要在有真实计算的程序里才明显(见Debug 与 Release 构建差异);这里的 hello world 编译时间都在 0.1 s 量级,差别主要来自「要不要生成可执行文件」。