Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cargo with features flag

I'm trying to learn rust by writing CLI but i can't do cargo run with features passed and i don't understand why. I read docs / stack and i still don't see why this is happening. It feels like it should work this way https://doc.rust-lang.org/cargo/commands/cargo-run.html

I'm trying to run this code

https://github.com/clap-rs/clap/blob/master/examples/17_yaml.rs

with command cargo run --features=yaml or cargo run --features yaml. I tried many combinations, none of them worked.

My Cargo.toml looks like that:

[dependencies.clap]
version = "*"
default-features = false
features = ["yaml"]

When i run i have error:

:!cargo run --features=yaml
error: Package `fun v0.1.0 (/Users/XXX/Projekty/rust/fun)` does not have these fe
atures: `yaml`

shell returned 101

What am i doing wrong?

like image 833
Kamil Staszewski Avatar asked Oct 22 '25 04:10

Kamil Staszewski


1 Answers

Their code expects you to have cloned the clap repository, changed into its directory, and then run cargo run --features yaml --example 17_yaml from there. You can read more about how the cargo examples feature works here.

If you’re planning on copying their code, as noted in that example code, you have to remove this conditional compilation attribute:

// Note: If you're using clap as a dependency and don't have a feature for your users called
// "yaml", you'll need to remove the #[cfg(feature = "yaml")] conditional compilation attribute
#[cfg(feature = "yaml")]
fn main() {

Otherwise it will load this other main implementation and emit that error:

#[cfg(not(feature = "yaml"))]
fn main() {
    // As stated above, if clap is not compiled with the YAML feature, it is disabled.
    println!("YAML feature is disabled.");
    println!("Pass --features yaml to cargo when trying this example.");
}

You don’t actually need to pass --features on the command line unless you are running their example within their crate as described above. You should also remove this whole function if you’re copying their code! It is only relevant when run as an example.

like image 198
ticky Avatar answered Oct 24 '25 18:10

ticky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!