Why is the `panic` in the if branch that has a true condition not executed if no `else` branch is supplied in `build.rs`?

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

surdeus

Warning

This post was published 125 days ago. The information described in this article may have changed.
// build.rs
fn main(){
    if std::env::var("DOCS_RS").is_ok(){
        panic!("{}",std::env::var("DOCS_RS").is_ok());
    }/*else{
        panic!("aaa");
    }*/
}

In PowerShell, $env:DOCS_RS="1"; cargo build can be successfully compiled, which is not expected. However, if you uncomment the comment

  --- stderr

  thread 'main' panicked at build.rs:3:9:
  true
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The expected error will be printed out. What's the reason here? It's confusing.

6 posts - 2 participants

Read full topic

🏷️ Rust_feed