Undocumented if-let behavior with or-patterns

未关闭 适合新手
#2,405 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
documentation

调研方向

该 issue 给出一个分两部分的 Rust 示例:一个带有或模式和 && 守卫的 if let,以及一个带有相同或模式和守卫的 match。运行两者以确认差异,然后在 Reference 中查找关于或模式、match 守卫、if let 和 let 链的章节。完成标准是 Reference 明确指出:当后续条件失败时,let 链不会重试或模式的各个分支,而 match 守卫会。

由索引模型根据 Issue 内容生成。

描述

Or-patterns in a match can cause a match guard to run multiple times, as each of the or'ed patterns gets tried after the previous one fails in the guard. However, with let-chains, there is no such "backtracking" behavior. This is not documented in the reference.

fn main() {
    if let (x, _) | (_, x) = (1, 2)
        && x == 2
    {
        println!("This is NOT printed.");
    }
    match (1, 2) {
        (x, _) | (_, x) if x == 2 => {
            println!("This is printed.");
        }
        _ => {}
    }
}

Relevant discussion: https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/Merging.20destructuring.20assignment.20and.20patterns/near/576565919

主要语言
Rust
星标
1.6k
派生
618
平均合并
5 小时 48 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/reference 的其他 Issue

查看 rust-lang/reference 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。