How can I find a crate dependency version?

Hi! I'd like to detect a lib dependency version in code, how could I do that?

Context: I'm in the process of creating a lib with shared functionality within my company, so other projects can use it.
For my bin projects, I use the git version which works wonderfully:

pub const VERSION: &str = git_version::git_version!(
    args = ["--tags", "--dirty", "--broken"],
    fallback = "out-of-tree"

But now that this lib crate is being used in Cargo.toml as my-lib = { path = "../my-lib" version = "0.1" }, that returns out-of-tree... So, I think I'll have to settle on the semver of the lib.
What I need is to log on the bin whether it was compiled with 0.1.0 or 0.1.35, for example:

        "Starting up version: {} (my-lib: ?)", crate::VERSION, my_lib::VERSION

Would it be possible?


Within the library's own code, you can get the package version as env!("CARGO_PKG_VERSION"). So, for your purpose:

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

See Environment Variables - The Cargo Book for more data you can obtain this way.

Ohh right!! I don't know how I didn't think that!
I got puzzled by the git's "out-of-tree" message, and only thought I'd have to compromise somehow... The semver version idea only came when I was writing the post.
Thank you @kpreid, that works nicely :+1:

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.