3 ms·
To make clearer what I meant by pattern bindings being available: if let Some(x) = y { println!("y has a value, {}", x); } Whereas with a method, th
by chrismorgan 8y ago
To make clearer what I meant by pattern bindings being available:
if let Some(x) = y {
println!("y has a value, {}", x);
}
Whereas with a method, the contents of the variant are not bound:
if y.is_some() {
println!("y has a value, {}", /* uh… */);
}
In Rust, pattern matching is the only to get at the contents of an enum. Methods like is_some() are just this:
pub fn is_some(&self) -> bool {
match *self {
Some(_) => true,
None => false,
}
}