How to get Cargo package metadata (version, name, description) in a Rust program?
Hello everyone! I'm starting to learn Rust and writing a small command-line utility. I want that when running with the `--version` flag, the program outputs not just the version number, but for example: "MyApp v0.2.1 (2024-05-20)".
The problem is that I don't want to hardcode the version in the code — that's wrong, since it's already specified in the `Cargo.toml` file. How can I properly extract this information at compile time or runtime?
I tried reading the `Cargo.toml` file directly, but that seems hacky, and the file might not be present in a release build. I've heard about the `env!()` macro, but I don't understand how to tie it to Cargo.
Here's an example of my current structure:
```rust
fn main() {
// How to get version from Cargo.toml?
println!("MyApp v{}", get_version());
}
```
I need to get: package name, version, description
No answers yet.
Log in to leave an answer