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

Download history 1520368/week @ 2025-11-01 1515030/week @ 2025-11-08 1574373/week @ 2025-11-15 1564053/week @ 2025-11-22 1660651/week @ 2025-11-29 1725045/week @ 2025-12-06 1672161/week @ 2025-12-13 1096612/week @ 2025-12-20 1092460/week @ 2025-12-27 1816006/week @ 2026-01-03 1946444/week @ 2026-01-10 1995332/week @ 2026-01-17 2120339/week @ 2026-01-24 2260742/week @ 2026-01-31 2444100/week @ 2026-02-07 2319713/week @ 2026-02-14

9,519,097 downloads per month
Used in 3,942 crates (879 directly)

MIT license

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.

Build Status crates.io

Documentation


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.7–1.8MB
~36K SLoC