Re:First Impressions of the Rust Programming Language
by chrismorgan on Sunday June 10, 2018 @05:25AM
(#17277314)
`if let Some(x) = y { … }` is equivalent in Rust to `match y { Some(x) => { … }, None => () }`: pattern bindings are available. Thus it is quite different from `if y.is_some() { … }`.
Re:First Impressions of the Rust Programming Language
by earenndil on Sunday June 10, 2018 @07:01PM
(#17280630)
Is it? It does something with y if it is something, otherwise it does nothing...
Re:First Impressions of the Rust Programming Language
by chrismorgan on Monday June 11, 2018 @08:04AM
(#17283546)
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,
}
}
Re:First Impressions of the Rust Programming Language
by nangtrongvuon on Sunday June 10, 2018 @01:41AM
(#17276753)
It’s also present in Swift.
AFAIK (not too familiar with rust or c++), you can do something like this: