How can a Rust program access metadata from its Cargo package?

How do you access a Cargo package’s metadata (e.g. version) from the Rust code in the package? In my case, I am building a command line tool that I’d like to have a standard --version flag, and I’d like the implementation to read the version of the package from Cargo.toml so I don’t have to maintain it in two places. I can imagine there are other reasons someone might want to access Cargo metadata from the program as well.

3 Answers
3

Leave a Comment