How to have an optional dependency enable another optional dependency rust

Viewed 792

I am creating a library in rust and I have different features which the user can enable via optional dependencies. I want it so if the dependency diesel is enabled than diesel-derive-enum should also be enabled.

[dependencies]
diesel = {version = "1.4.6", optional = true, features = ["postgres", "chrono"]}
diesel-derive-enum = {version = "1.1.1", optional = true, features = ["postgres"]}
2 Answers

As of Rust 1.60.0, with the stabilization of namespaced dependencies, you can now do this without having to add a differently named feature, using dep: features.

[dependencies]
diesel = {version = "1.4.6", optional = true, features = ["postgres", "chrono"]}
diesel-derive-enum = {version = "1.1.1", optional = true, features = ["postgres"]}

[features]
diesel = ["dep:diesel", "dep:diesel-derive-enum"]

Now you can simply pass --features diesel to cargo to enable both the diesel and diesel-derive-enum optional dependencies.

See the Cargo Book's section on Optional Features for more details.

Related