3 ms·
> if let is a syntax construct that I don't think any other language has AFAIK (not too familiar with rust or c++), you can do something like this: if (Ma
by earenndil 8y ago
> if let is a syntax construct that I don't think any other language has
AFAIK (not too familiar with rust or c++), you can do something like this:
if (Maybe may = whatever; may.issome)
- nangtrongvuon 8y agoIt’s also present in Swift.
- chrismorgan 8y ago`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() { … }`.
- earenndil 8y agoIs it? It does something with y if it is something, otherwise it does nothing...
- chrismorgan 8y agoTo 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, } }