3 ms·
"if let" is rusts answer to Java's "isPresent" method. if let Some(val) = my_optional_value { // perform some side effect with val }
by LAC-Tech 2y ago
"if let" is rusts answer to Java's "isPresent" method.
if let Some(val) = my_optional_value {
// perform some side effect with val
}
- brabel 2y agoIn the context of this blog post, an example with Result would be more suitable though. if let Ok(value) = fun_can_fail() { // success, use value } else { // error, but we don't care which one when using if-let }
- Expurple 2y agoThis isn't the only pattern that `if let` is suitable for. It's often used without an `else`, as a syntax sugar for match value { the_only_case_that_we_care_about => { // Do something with the bound value. }, _ => {}, // Don't do anything, we don't care about the other cases at all. }