这是我的Cargo .
[package]
name = "tmp"
version = "0.1.0"
edition = "2021"
[dependencies]
chrono = "0.4.24"
chrono-tz = {version="0.8.2", optional = true}
[features]
my_feature = ["chrono-tz"]
以下是我的src/main.rs
条建议:
#[cfg(feature = "some_feature")]
use chrono_tz::Tz;
#[cfg(feature = "some_feature")]
fn my_fn(tz: Tz){
println!("tz is: {:?}", tz);
}
fn feature_function(tz: Option<Tz>) {
match tz {
#[cfg(feature = "some_feature")]
Some(tz) => my_fn(tz),
_ => (),
}
}
fn main() {
feature_function(None);
}
注意feature_function
是如何接受tz: Optional<Tz>
的,但是只有在启用了"SOME_FEATURE"时才会达到Some(tz)
的情况.
因此,如果我没有启用"SOME_FEATURE",我希望能够用tz=None
呼叫feature_function
.
可以预见的是,这不会编译:
error[E0412]: cannot find type `Tz` in this scope
--> src/main.rs:10:32
|
10 | fn feature_function(tz: Option<Tz>) {
| ^^ not found in this scope
|
help: you might be missing a type parameter
|
10 | fn feature_function<Tz>(tz: Option<Tz>) {
| ++++
For more information about this error, try `rustc --explain E0412`.
error: could not compile `tmp` due to previous error
有没有什么技巧可以用来在feature_function
中输入tz
,这样如果没有启用"SOME_FEATURE",我仍然可以用tz=None
呼叫feature_function
?