[英]Why don't Option's Some and None variants need to be qualified?
According to the docs for Option
, Option
is an enum with variants Some<T>
and None
. 根据Option
的文档 , Option
是一个包含变量Some<T>
和None
的枚举。
Why is it possible to refer to Some
and None
without qualifying them? 为什么可以在没有资格的情况下引用Some
和None
?
For example, this works fine: 例如,这工作正常:
let x = Option::Some(5);
match x {
Some(a) => println!("Got {}", a),
None => println!("Got None"),
}
But this fails to compile: 但这无法编译:
enum Foo<T> {
Bar(T),
Baz,
}
let x = Foo::Bar(5);
match x {
Bar(a) => println!("Got {}", a),
Baz => println!("Got Baz"),
}
The error from the compiler is unresolved enum variant, struct or const `Bar`
编译器的错误是unresolved enum variant, struct or const `Bar`
The Rust prelude , which is automatically inserted into every source file, contains this line: Rust prelude (自动插入到每个源文件中)包含以下行:
pub use option::Option::{self, Some, None};
Which brings Option
and both its variants in scope. 这带来了Option
及其变体的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.