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 1097195/week @ 2025-12-28 1840386/week @ 2026-01-04 1949876/week @ 2026-01-11 1996732/week @ 2026-01-18 2137487/week @ 2026-01-25 2303038/week @ 2026-02-01 2452152/week @ 2026-02-08 2322085/week @ 2026-02-15 2457206/week @ 2026-02-22 3134689/week @ 2026-03-01 3093574/week @ 2026-03-08 2782408/week @ 2026-03-15 2656033/week @ 2026-03-22 2770342/week @ 2026-03-29 2912731/week @ 2026-04-05 2986294/week @ 2026-04-12

11,553,083 downloads per month
Used in 4,219 crates (948 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.7MB
~34K SLoC