如何使两个功能启用导致 Rust 发生冲突?

Vic*_*voy 4 rust rust-cargo

我有两个特点:feature_1feature_2

[features]
default = ["feature_1"]
feature_1 = []
feature_2 = []
Run Code Online (Sandbox Code Playgroud)

我想让用户一次只选择其中一个,因为同时选择两者会导致一些重要代码的重复以及其他一些原因。我怎样才能做到这一点?

小智 9

考虑到它的价值,我选择使用这种方法:

#[cfg(all(feature = "feature_1", feature = "feature_2"))]
compile_error!("Feature 1 and 2 are mutually exclusive and cannot be enabled together");
Run Code Online (Sandbox Code Playgroud)

我希望它能帮助其他人寻找相同问题的解决方案。