From 5124a9efcea180c655858fd00a174af851ee800c Mon Sep 17 00:00:00 2001 From: Rustln Date: Fri, 25 Feb 2022 17:11:46 +0800 Subject: [PATCH] Add `matches!` Individual examples. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对读者首次接触**matches!宏**的例子进一步说明,因为**匹配守卫**在**2.6.4. 全模式列表**章节才有介绍。 --- contents/basic/match-pattern/match-if-let.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/basic/match-pattern/match-if-let.md b/contents/basic/match-pattern/match-if-let.md index 929c622f..1cb4a0d3 100644 --- a/contents/basic/match-pattern/match-if-let.md +++ b/contents/basic/match-pattern/match-if-let.md @@ -299,6 +299,8 @@ let bar = Some(4); assert!(matches!(bar, Some(x) if x > 2)); ``` +表达式 `Some(x) if x > 2` 中 `if` 能为分支模式提供更进一步的匹配条件,在模式匹配中简称**匹配守卫**(match guard)。 + ## 变量覆盖 无论是是 `match` 还是 `if let`,他们都可以在模式匹配时覆盖掉老的值,绑定新的值: ```rust