Recomendations for common source files

I have a workspace with multiple projects. I have a file of constants that is needed by them all.

Whats the accepted best practice

  • compile it to a lib - seems a little heavy
  • can I make a 'mod constants;' statement somehow point outside the immediate src tree
  • other?

You can make another library in your workspace. This is the simplest and usually works fine. Or you can make your code generic over the constants, and then provide the constants in the top level crate.

2 Likes

This is possible with the #[path] attribute.

However, this will not work if you intend to cargo publish your packages, since the file would not be included in the package. You can use symlinks instead (packaging automatically replaces symlinks out of the package with the file contents), if you only intend to develop or publish your packages from Unix systems and not Windows. Or, if you don't intend to publish at all, it doesn't matter.

2 Likes

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.