Is there any way to get all available features of a crate?

For example to Node.JS I could run npm view http-server:

For rust I saw this command cargo metadata but with it I can see only the informations for a crate that I have installed.

1 Like

Currently not builtin, it's available as a separate crate: cargo-info.
There are plans to merge it into cargo proper.

8 Likes

The lib.rs site has pages for crate features, e.g.:

7 Likes

I read the Cargo.toml for the features and then grep/rg the source for the cfg's using the features I am interested in. Also a rg/grep for just the feature word grabs the stuff in comments and README or other places that might mention the feature.
For example if I were looking in rust core library the Cargo.toml shows only a couple of features. "debug_refcell" looks interesting.

$/source/rust/library/core$ rg 'cfg.*debug_refcell'
src/cell.rs
706:    #[cfg(feature = "debug_refcell")]
715:    #[cfg(feature = "debug_refcell")]
<SNIP>

Reading the file src/cell.rs I see that feature basically adds some fields to some structs provides some Debug impl and error passing. *I don't understand it all, but enough to only need a minute to realize I am not interested in that feature "yet"

Sorry, I re-read your question and I see you are interested in stuff you don't have locally installed.

If you are in a Cargo project directory, cargo add --dry-run <crate> will list the features of a crate.

(You can shorten this to cargo add -n <crate>.)

9 Likes

To know all features for a crate is not hard, docs.rs provides the features page for each crate, like tokio 1.36.0 - Docs.rs (and raw Cargo.toml if you want to check it).

The entrypoint is the FeatureFlags button on top bar. But the button can be enhanced by Rust Search Extension so that you don't have to open a new page to view features.

If you use IDE plugins targeting interactive feature selection in Cargo.toml, you'd get better user experience when choosing features, since they help you enable features that won't overlap with each other for one dependency (not for global dep graph though).

docs.rs only hosts docs by enabling default features (or rather one kind selection by specifying [package.metadata.docs.rs]).

I'm also working on a TUI to compile docs based on interactive features selection, as a result, you can easily view multiple docs for a crate with various features enabled. GitHub - zjp-CN/term-rustdoc: [WIP] A TUI for Rust docs that aims to improve the UX on tree view and generic code.

FeatureSelection

4 Likes

I love these solutions just to shared another possible solution using curl and jq!

curl https://crates.io/api/v1/crates/<crate> | jq '.versions[0].features'

Basically the cargo info uses this api!

Amazing it was really helpful! Thank you!

Amazing probally it is the best solution for now! Thank you!

The one on track to merge is cargo-information -- the command is still cargo info though.

3 Likes

How can I check which one is really going to be merged?

Note that @hi-rustin (author of cargo-information) is also involved here:

1 Like

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.