64 releases (21 breaking)
| 0.23.1 | Nov 12, 2025 |
|---|---|
| 0.22.0 | Aug 18, 2025 |
| 0.21.0 | Jul 10, 2025 |
| 0.19.2 | Mar 2, 2025 |
| 0.2.0 | Mar 17, 2017 |
#1 in Cargo plugins
6,060,316 downloads per month
Used in 3,651 crates
(826 directly)
79KB
1K
SLoC
cargo_metadata
Structured access to the output of cargo metadata. Usually used from within a cargo-* executable.
Also supports serialization to aid in implementing --message-format=json-like
output generation in cargo-* subcommands, since some of the types in what
cargo --message-format=json emits are exactly the same as the ones from cargo metadata.
lib.rs:
Structured access to the output of cargo metadata and cargo --message-format=json.
Usually used from within a cargo-* executable
See the cargo book for details on cargo itself.
Examples
Get the current crate's metadata without default features but with all dependency information.
let _metadata = MetadataCommand::new().exec().unwrap();
If you have a program that takes --manifest-path as an argument, you can forward that
to [MetadataCommand]:
let mut args = std::env::args().skip_while(|val| !val.starts_with("--manifest-path"));
let mut cmd = MetadataCommand::new();
let manifest_path = match args.next() {
Some(ref p) if p == "--manifest-path" => {
cmd.manifest_path(args.next().unwrap());
}
Some(p) => {
cmd.manifest_path(p.trim_start_matches("--manifest-path="));
}
None => {}
};
let _metadata = cmd.exec().unwrap();
Pass features flags, e.g. --all-features.
let _metadata = MetadataCommand::new()
.manifest_path("./Cargo.toml")
.features(CargoOpt::AllFeatures)
.exec()
.unwrap();
Parse message-format output produced by other cargo commands.
It is recommended to use crates like escargot to produce the [Command].
let mut command = Command::new("cargo")
.args(&["build", "--message-format=json-render-diagnostics"])
.stdout(Stdio::piped())
.spawn()
.unwrap();
let reader = std::io::BufReader::new(command.stdout.take().unwrap());
for message in cargo_metadata::Message::parse_stream(reader) {
match message.unwrap() {
Message::CompilerMessage(msg) => {
println!("{:?}", msg);
},
Message::CompilerArtifact(artifact) => {
println!("{:?}", artifact);
},
Message::BuildScriptExecuted(script) => {
println!("{:?}", script);
},
Message::BuildFinished(finished) => {
println!("{:?}", finished);
},
_ => () // Unknown message
}
}
let output = command.wait().expect("Couldn't get cargo's exit status");
Dependencies
~0.8–1.9MB
~38K SLoC