Category Archives: crates

Publishing my first Rust library to crates.io

As part of my end of 2021 project, I rewrote my F# units of measurement converters to a format which allowed me to code generate a bunch of libraries in different languages, currently including the original F#, C# (both idiomatic to their language), Java, Python, Swift, TypeScript and Rust.

I’ve got the F# and C# ones available on nuget and the TypeScript on npmjs, I wanted to try to get the Rust library onto crates.io.

How do we do this?

  • First off, check if your package name is available on crates.io, if it is then you’ll need to change your name. Rust creates do not support namespaces so you cannot reuse an existing name but prefix with your company or whatever
  • Load up https://crates.io/ and create an account, I think using GitHub to login was the only option, so that’s what I did
  • Ensure you have an email within your profile page and that it’s verified
  • You can go to the API Tokens page and create a new token and a name for the token – you’ll need to copy the API token for the publish step (and keep it for further publishes unless you wish to revoke and regenerate – obviously not too useful in CI. If you set the environment variable CARGO_REGISTRY_TOKEN to the token then cargo will use it from there.
  • Ensure your code is committed (if you’re using source control) or you can always specify the –allow-dirty option if you prefer not to commit changes
  • Ensure you lib.rs has the correct version for your library setup. Semantic versioning is expected, hence you can use -alpha or the likes on your version
  • Now run
    cargo login abcdefghijklmnopqrstuvwxyz
    

    replacing abcdefghijklmnopqrstuvwxyz with your API token

  • Next run
    cargo publish
    

If all the above steps went well you can now search crates.io for your library.