如何使用 cfg 排除架构?

问题描述 投票:0回答:1

我正在开发一个应针对 wasm 的项目,但某些功能将不受支持。

看起来我可以通过这种方式有条件地包含一个函数:

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

或者有条件地这样称呼它:

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

但是我如何有条件地排除 wasm 上的声明或代码块?

即我正在寻找类似于 c 宏中的

#ifndef
的东西:

#ifndef WASM
native_only_func();
#endif
rust conditional-compilation wasm-bindgen rust-wasm
1个回答
2
投票

要否定条件,请使用

#[cfg(not(condition))]
。您可以在 The Rust Reference 的 this 部分阅读有关条件编译的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.