What's the convention for handling a hybrid library and binary crate's dependencies

I'm developing a crate keepawake that has both a library crate and binary crate. Rust allows a single crate to have both a library and binary part, yet, a problem still remains, you can't easily declare binary only dependencies, see rust-lang/cargo#1982 (h????://github.com/rust-lang/cargo/issues/1982). This leads to the library crate carrying dependencies that it doesn't really use (e.g. clap) which are troublesome for users of the library crate (segevfiner/keepawake-rs#3 (h????://github.com/segevfiner/keepawake-rs/issues/3)).

I'm not sure what's the best way to handle this until something better is implemented in Cargo. I found rust - How can I specify binary-only dependencies? - Stack Overflow, but I'm not sure which solution of the ones offered there is best. And should I choose to use a workspace, what should the name of the library crate and binary crate be (Is there a convention)? As that will require creating a crate solely for splitting those dependencies with an artificial name, e.g. keepawake-cli.

Links mangled due to dumb forum software restriction. :frowning_face:

I think you just have to rename your binary crate to something like keepawake-bin. Or if not too many people use the library you may do it the other way around. But yes, it sucks.

One way is to make the binary and it's dependencies a feature, like this:


name = "my-binary"
required-features = ["commandline"]

commandline = ["clap"] // or anything you need for binary only

clap = { version ... , optional = true }

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.