Publishing crate with lib and binary

I'm new in Rust, I still don't know very well which are the best strategies to publish crates and what users expect in a crate.

So... I have a project, which is a web template engine, it works as a crate, it is published and everything is fine, but one of the parts is an IPC, which will be an executable, necessary for the template engine to work in other programming languages.

This is the structure I have planned:

neutralts/
└── src/        
│   └── lib.rs    
 ── ipc/
    └── src/
        └── main.rs  # bin

Questions:

Is it acceptable?, if so is it a good idea?, is a separate project for IPC a better idea?, not least because Rust users don't need it.

This layout won't work with crates.io. The crates.io package will see only the top-level src, and the ipc dir will be dead code, impossible to use.

If you want, you can have both library and binary in the same crate packaged together on crates.io. It will work as a library when added as a dependency, and it will work as a binary when used with cargo install. To make this work, create /src/main.rs instead of a separate crate in a subdir. The downside is that you only get one set of features and dependencies for all build targets (library and binaries) in the same package, and usually binaries need extra dependencies that libraries don't. Also be careful not to use mod in the main.rs, since that will duplicate code (the binary always gets linked with its library from the same package).

When the binaries are optional or need different deps, the standard solution is to make a workspace with separate library-only and binary-only crates (the binary one with a -cli suffix).

2 Likes

As I was saying, in reality the binary will not be used by crate users, in fact by any rust user. It is for using the library with other programming languages.

In any case what I think I understand is that it is better to have a totally separate crate and add there the necessary dependencies to my library, is that it?

I'm confused about who runs the binary. If I wanted to use your library with another programming language, would I need to run the binary?

It is a Web template engine, the idea is to be “language-agnostic”, so that the templates created are compatible with any system or programming language.

So it requires an IPC, a server, an equivalent would be the operation of php fpm.

Edit:
If you are curious you can see it here:
crates.io: Rust Package Registry probably next week the IPC will be published and you will be able to see how it works.

If your intent is to publish a crate to crates.io such that a user with cargo installed on their system can install your binary by running cargo install [crate name], so as to download the source code and build it from source on their machine using an installed copy of the rust compiler on their machine, you can do as kornel described.

If your intent is to publish some pre-built binary so that users without a rust compiler installed on their machine can download and run it, crates.io is not meant for that. Crates.io is meant as an internal system for developers using Rust, and is not intended as a general-purpose package manager. If this is what you wish to do, I suggest you do something like, add your compiled executable to a GitHub release, or look into how to do this with a general-purpose package management system of your choice such as flatpak or apt or something.

1 Like

I think I have my answer, thanks!