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 1516653/week @ 2025-10-21 1526087/week @ 2025-10-28 1522276/week @ 2025-11-04 1515651/week @ 2025-11-11 1690685/week @ 2025-11-18 1463392/week @ 2025-11-25 1700736/week @ 2025-12-02 1710978/week @ 2025-12-09 1626655/week @ 2025-12-16 1044580/week @ 2025-12-23 1197462/week @ 2025-12-30 1891530/week @ 2026-01-06 1895202/week @ 2026-01-13 2103984/week @ 2026-01-20 2132288/week @ 2026-01-27 2370453/week @ 2026-02-03

8,840,865 downloads per month
Used in 3,852 crates (862 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