Does "rustc-cfg=docsrs" set both RUSTFLAGS and RUSTDOCFLAGS?

⚓ Rust    📅 2025-07-31    👤 surdeus    👁️ 13      

surdeus

Warning

This post was published 125 days ago. The information described in this article may have changed.

I'm a bit confused by what "rustc-cfg=docsrs"` in build.rs exactly does. Consider this example:

//lib.rs 

#[cfg(not(docsrs))]
pub fn add(){}

If running cargo doc --open in the PowerShell with just setting $env:RUSTFLAGS="--cfg docsrs", the generated documentation includes the item add. To make the #[cfg(not(docsrs))] works under documentation building, we should manually set $env:RUSTDOCFLAGS="--cfg docsrs". Because RUSTDOCFLAGS="--cfg docsrs" will be passed to rustdoc while RUSTFLAGS="--cfg docsrs" will be passed to rustc. (I don't know whether this understanding is right.)

However, if adding the build.rs and using the following script.

// builds.rs
fn main(){
  println!("cargo:rustc-cfg=docsrs");
}

when building the documentation, this does work as if we had manually set $env:RUSTDOCFLAGS="--cfg docsrs". I'm confused by that, from the literal, rustc-cfg=docsrs, shouldn't rustc-cfg equal RUSTFLAGS="--cfg docsrs"? Why does it also behave as like setting RUSTDOCFLAGS="--cfg docsrs"?

1 post - 1 participant

Read full topic

🏷️ Rust_feed