Pixel Coder
Pixel Coder

Reputation: 329

How to use Cargo commands programmatically in Rust?

I want to use Cargo commands (cargo build in this instance) programmatically without running the command with std::process::Command in Rust.

How can I do that?

Upvotes: 5

Views: 2171

Answers (1)

loops
loops

Reputation: 5725

Add the cargo (docs) crate to your dependencies, and you can use Cargo as a library! It doesn't have any stability guarantees or much documentation though, so you might want to read through how the Cargo CLI tool uses the library to figure out how to use it. To do a build, cargo::ops::compile is the function to call.

But do keep in mind that the Cargo library will still run rustc in another process using Command::new, so this is really just moving where the process boundary is, not eliminating it entirely.

Upvotes: 16

Related Questions