Skip to content

Guards example may be confusing #1539

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
skade opened this issue May 10, 2022 · 4 comments
Closed

Guards example may be confusing #1539

skade opened this issue May 10, 2022 · 4 comments

Comments

@skade
Copy link

skade commented May 10, 2022

In https://doc.rust-lang.org/rust-by-example/flow_control/match/guard.html, it is stated that:

Note that the compiler does not check arbitrary expressions for whether all possible conditions have been checked. Therefore, you must use the _ pattern at the end.

I was teaching a class today where someone interpreted this sentence as _ being mandatory when using guard expressions. They were weirded out because of an example where I bound to a variable instead (bc. the guards weren't covering) and didn't know that was possible.

@balroggg
Copy link
Contributor

@skade In this case the compiler can not guarantee a matching pattern. Maybe the example should be

_ => unreachable!("Negative number is not supported"),

to indicate error case

@xiaochuanyu
Copy link
Contributor

@skade / @balroggg I made a PR to try address this.
Take a look and let me know what you think.

@balroggg
Copy link
Contributor

balroggg commented Jun 9, 2022

@xiaochuanyu LGTM

@marioidival
Copy link
Member

Thank you guys!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants