Why does crates.io suggest add to toml for bin crates?

I just noticed crates.io suggests for my bin crate

Install
Add the following line to your Cargo.toml file:
rustdb-axum-example = "0.1.36"

when for a binary crate, what you do is run "cargo install (package-name)", you don't add anything to a Cargo.toml file?

Am I doing something wrong, or is crates.io misleading, or am I somehow confused?

That's there for every crate. It's a convenient way to copy and paste the right toml to add a crate you found on crates.io to your project without having to figure out what version you want

1 Like

Yes, but for a bin crate ( a program ), rather than a library ( lib ) crate, it cannot be added to a project, can it? Or maybe it can, perhaps that is what I am missing.

Hmm, googling, I found this:

https://rust-lang.github.io/rfcs/3028-cargo-binary-dependencies.html

I assume the implementation is just too simplistic, and doesn't check if the crate has a lib or not. This information isn't available directly in the registry, and requires analyzing crate's tarball, so it's not surprising it's not done.

In lib.rs I check this, and don't display Cargo.toml section if it's not applicable:

4 Likes

A package uploaded to crates.io can contain both a binary and a library, right? e.g. I think this is how bindgen works (you can cargo install bindgen or add bindgen = "0.60" to your Cargo.toml) so just because a crate has a binary doesn't mean it can't also be useful to provide the dependancy suggestion.

1 Like

When you, as the author of a program, prepare a binary crate for distribution, it does include a Cargo.toml file describing the program's build configuration and dependencies.

When you, as the end user of a program, install a binary crate, you do not provide a Cargo.toml. Cargo will use the metadata provided by the author (as above) to determine what needs to be installed in order for the program to work on your machine.

Two different use cases. That message targets the author, not the end user. There are situations where someone authoring a crate might want to depend on a binary crate, though they are rare.

Yes, this is what I didn't know. I think ideally perhaps the manifest should indicate somehow which kind of binary crate is being published ( an "ordinary" program or a "build tool" ), so that appropriate instructions are displayed?

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.