Programmatically finding an example binary

The tests of my library partially use a binary provided by an example. Works on stable for now, but with the recent layout changes, beta/nightly fail because of path issues.

Is there a good way to find where the binary of the example resides (after having called cargo build --example basic)? Right now, I'm doing

PathBuf::from(env::var("OUT_DIR").unwrap())
    .join("../../../examples")

which apparently is not portable between the stable/beta/nightly channels. I could specialise on the cargo version or something, but is there a more programmatic way? I was hoping for an env var like EXAMPLES_BINARY_DIR, but did not find anything that's helping here.

Thanks for any pointers :slight_smile:

There's the CARGO_BIN_EXE_<name> environment variable for binaries.
That's available to integration tests, but only for binaries, not examples.
The assert_cmd crate makes use of it.

While not directly a solution, you led me down the path of json output of cargo, which made me notice the example binary is indeed in the same place for stable/beta/nighty, I just needed to use a different env var for that. I went with

PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
    .join("target/debug/examples")

which fixed the issue. Thanks!