How can I locate resources for testing with Cargo?

The environment variable CARGO_MANIFEST_DIR can give you a stable base point to reference other files. Here, we assume that there’s a resources/test directory at the top level of the crate:

use std::path::PathBuf;

fn main() {
    let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    d.push("resources/test");

    println!("{}", d.display());
}

See also:

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)