rustconditional-compilationwasm-bindgenrust-wasm

How do I exclude an architecture with cfg?


I'm working on a project which should target wasm, but some functionality will not be supported.

It looks like I can conditionally include a function in this way:

#[cfg(target_arch = "wasm32")]
fn my_func() { ... }

Or conditionally call it like so:

if cfg!(target_arch = "wasm32") {
    my_func();
} else {
    ...
}

But how can I conditionally exclude a declaration or a block of code on wasm?

I.e. I am looking for something similar to #ifndef in c macros:

#ifndef WASM
native_only_func();
#endif

Solution

  • To negate condition use #[cfg(not(condition))]. You can read more about conditional compilation in this section of The Rust Reference.