What is the best way to use a crate dependency of another crate in your crate?

I have a real-life example. I have a discord bot which uses discord-rs crate library to work with the service. The discord-rs crate uses chrono for itself for the dates. In my crate, which uses discord-rs I need to initialize a structure field of discord-rs with a chrono::DateTime type but my crate does not have chrono dependency. What options do I have?

1 Like

If the discord crate does not export chrono you have to add chrono as a dependency of your own project (and make sure you're using the same version)

So we have only two options, right? I mean, reexport crate and to use the same dependency crate version in your crate.

Right, you can't access dependencies of dependencies any other way.

If you are using a dependency of a dependency, then you are directly depending on it so it should be your dependency as well.

If you don't, tomorrow if the dependency drops it as a dependency, then you will end up with broken code.


There are some times when the dependency chain is implicit and feels it always will be. For example if you have a physics verlet integrator, it depends on glam (a 3d math lib) it means anything that takes the verlet integrator also has to take glam, manually, in the toml.
It'd be nice, and save a serious amount of typing, if you could create a verlet crate which automagically included the glam dependency.

It'd be nice to write something like verlet::position and it'd works in the test app, rather than having to include all of the chained dependencies down?

That's what re-exporting is for. The verlet crate can pub use ::glam::{ /* ... */ }, and all of those symbols will be available to the main program.

Thanks. I&I is still very early with Rust.

This topic was automatically closed after 7 days. We invite you to open a new topic if you have further questions or comments.