Skip to content

Different default features for crate binary and library #12054

@emilk

Description

@emilk

Problem

I have a crate that is both a library and a binary. I want to be able to specify a default set of features for users of the library and the binary. For instance, people who write mycrate = "*" in their Cargo.toml get a small set of features (and fast compilation times), but when typing cargo install mycrate they get most or all of the features enabled.

This is currently not possible, due to the single [features] default = […] field in Cargo.toml.

Proposed Solution

The [lib] and [[bin]] sections already have a required-features = […] field. I propose we also add a default-features = […] field that, if present, will override the default field in [features]:

[package]
name = "mycrate"

[[bin]]
# Make most features opt-out for users of the binary
default-features = ["features", "that", "binary", "users", "often", "want"]

[lib]
default-features = [] # Make all features opt-in for users of the library

[[test]]
# We don't set `default-features` here, so the same features from `[features] default` are used


[features]
# default features for any target that hasn't set `default-features` (tests, examples and benches):
default = ["foo", "bar"]

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions