Are there any Environment Variables set by rustc?


#1

Cargo sets a bunch of environment variables for build time, which expose many of the fields defined in manifest’s package table.

My question is, are there any environment variables defined by rustc itself? I’m searching around, but can’t find an answer to this.

Specifically, I like to know if I can grab the crate name during compile time, as known to the complier.

I should add that, I’m not talking about those env-vars used by rustc, but those set by rustc to be used in the code.


#2

I don’t think rustc sets any environment variables, instead it has a bunch of internal macros which will do roughly the same. For example there is line!(), column!(), file!(), and module_path!(). In particular you could use that last one to extract the name of the current crate.

macro_rules! crate_name {
    () => {module_path!().split("::").next().unwrap()}
}

#3

Thanks, @Michael-F-Bryan! Yeah, I didn’t think of using module_path!(). Apparently using it this way is also highlighted in the docs! :slight_smile: