返回文章列表

文章

关于Cargo.toml中的workspace配置

在 Cargo 1.68+ 引入的“工作区继承”(workspace inheritance)机制里,凡是在 Cargo.toml 里写成

[package]
name = "egui_demo_app"
version.workspace = true
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
license.workspace = true
edition.workspace = true
rust-version.workspace = true
publish = false
default-run = "egui_demo_app"

[package.metadata.cargo-machete]
ignored = ["profiling"]

[lints]
workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--generate-link-to-definition"]

[lib]
crate-type = ["cdylib", "rlib"]


[features]
default = ["glow", "persistence"]

# image_viewer adds about 0.9 MB of WASM
web_app = ["http", "persistence"]

http = ["ehttp", "image/jpeg", "poll-promise", "egui_extras/image"]
image_viewer = ["image/jpeg", "egui_extras/all_loaders", "rfd"]
persistence = [
  "eframe/persistence",
  "egui_extras/serde",
  "egui/persistence",
  "serde",
]
puffin = ["dep:puffin", "dep:puffin_http", "profiling/profile-with-puffin"]
serde = ["dep:serde", "egui_demo_lib/serde", "egui/serde"]
syntect = ["egui_demo_lib/syntect"]

glow = ["eframe/glow"]
wgpu = ["eframe/wgpu", "bytemuck", "dep:wgpu"]
wayland = ["eframe/wayland"]
x11 = ["eframe/x11"]

[dependencies]
chrono = { version = "0.4", default-features = false, features = [
  "js-sys",
  "wasmbind",
] }
eframe = { workspace = true, default-features = false, features = [
  "web_screen_reader",
] }
egui = { workspace = true, features = ["callstack", "default", "log"] }
egui_demo_lib = { workspace = true, features = ["default", "chrono"] }
egui_extras = { workspace = true, features = ["default", "image"] }
image = { workspace = true, default-features = false, features = [
  # Ensure we can display the test images
  "png",
] }
log.workspace = true
profiling.workspace = true

# Optional dependencies:

bytemuck = { workspace = true, optional = true }
puffin = { workspace = true, optional = true }
puffin_http = { workspace = true, optional = true }
# Enable both WebGL & WebGPU when targeting the web (these features have no effect when not targeting wasm32)
wgpu = { workspace = true, features = ["webgpu", "webgl"], optional = true }


# feature "http":
ehttp = { version = "0.5", optional = true }
poll-promise = { version = "0.3", optional = true, default-features = false }

# feature "persistence":
serde = { workspace = true, optional = true }


# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
env_logger = { version = "0.10", default-features = false, features = [
  "auto-color",
  "humantime",
] }
mimalloc.workspace = true
rfd = { version = "0.15.3", optional = true }

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "=0.2.97"
wasm-bindgen-futures.workspace = true
web-sys.workspace = true

[dev-dependencies]
egui_kittest = { workspace = true, features = ["eframe", "snapshot", "wgpu"] }

就表示 “不用在本包里写具体数值,改成从工作区根目录的配置里继承”。也就是说:

  • 在工作区根的 Cargo.toml(通常是同目录下上一级)里,你会有一段类似:

[workspace.package] version = "0.1.0" license = "MIT OR Apache-2.0" edition = "2021" rust-version = "1.65" ```

  • 然后子包里就可以把这些字段写成 .workspace = true,Cargo 在编译时会自动帮你把 versionlicenseeditionrust-version 等值补上,确保所有成员包都用同一套版本号、同一套许可、同一套 Rust 编辑版和最低 Rust 版本要求。 好处是:
  1. 统一维护:只需在顶层 workspace.package 里改一次,所有包都自动跟着更新。
  2. 减少重复:子包不用再粘贴重复的版本号、许可声明之类。
  3. 避免不一致:多人协作时更不容易把子包的版本漏改或者写错。 所以 .workspace = true 并不是个“特殊值”,而是告诉 Cargo “这个字段从工作区继承,不要在本地再写具体值”。