Reputation: 195
According to the docs for Option
, Option
is an enum with variants Some<T>
and None
.
Why is it possible to refer to Some
and None
without qualifying them?
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`
Upvotes: 16
Views: 2135
Reputation: 15002
The Rust prelude, which is automatically inserted into every source file, contains this line:
pub use option::Option::{self, Some, None};
Which brings Option
and both its variants in scope.
Upvotes: 25