Need help with turning app into lib

Hi everyone. I have a project that will have v4.0.0 version soon. And I published it only on github. But I want to re-make it into library and publish on crates.io.

So, I have the next questions:

  1. which version should I publish. Current actual version v4.0.0 or it should be version 0.1.0 ?
  2. except renaming main.rs into lib.rs which other steps should I do ?

I would stick with 4.0.0 since that is consistent with your past commits. The only reason to make it 0.x is if you need to indicate that the crate is unfinished, but >=1.0.0 isn't any sort of guarantee so it's up to you. You could also use non-numeric versions like "4.0.0-alpha1".

Usually what you would do is:

  • Rename main.rs to lib.rs.
  • Move the main function to a fresh main.rs. This allows you to have a library and binary side by side. Unless you want to drop the binary. Factor out some parts of main if you want them to be public, and leave them in lib.rs.
  • Make most of the things in lib.rs public, both for people using your crate and your main.rs.
  • Add use statements in main.rs for anything you use there. You will need to import them using your crate name, so something like use your_crate_name::Item; Don't put mod declarations here unless there are modules that only your binary should have access to; those declarations should be removed from lib.rs. Modules should only be declared in one place, and everywhere else should use them.

In general, when publishing a library it's good to go through the API guidelines checklist.

4 Likes

Thank you !