if you modified build.rs, cargo will automatically (rebuild and) re-run the build script. but if you only change the environment variable without changing the build script (or any ./src/* files), cargo build will not re-run the build script (unless it's a clean build).
Okay. I still found a confusion part. If I manually set $env:RUSTFLAGS="--cfg docsrs"; cargo doc --open, then I can still see the item in the generated documentation, where the item in the source code is written as:
// lib.rs
#[cfg(not(docsrs))]
pub fn add(x: i32, y: i32) -> i32 {
x + y
}
However, if I used the build.rs, for example,
fn main(){
println!("cargo:rustc-cfg=docsrs");
}
Then, the generated documentation won't include the add item? Shouldn't rustc-cfg equal RUSTFLAGS="--cfg docsrs"?
PS: If I want to manually set docsrs, I need to set RUSTDOCFLAGS="--cfg docsrs". Why does rustc-cfg in build.rs affect RUSTDOCFLAGS?
this, I don't really know, presumably cargo is hanlding the environment variables before invoking rustdoc.
I wonder does it behave differently between cargo doc and cargo rustdoc? I vaguely remember there's different hanlding for the flags between cargo build and cargo rustc, maybe it's similar for the case of documentation?