2.5.1 `if let` 表达式
01-
if let 表达式 — Comprehensive Rust1分钟内可阅读完
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/pattern-matching/let-control-flow/if-let.html
2.5.1 if let 表达式
if let 表达式
可以根据值是否匹配某个模式来执行不同代码:
| |
- 与
match不同,if let不必覆盖所有分支,因此有时比match更简洁。- 常见用法是处理
Option中的Some值。- 与
match不同,if let不支持用于模式匹配的守卫子句。- 带上
else子句时,它可以作为表达式使用。