-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Updated "while let" example. #30059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated "while let" example. #30059
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ if let Some(x) = option { | |
## `while let` | ||
|
||
In a similar fashion, `while let` can be used when you want to conditionally | ||
loop as long as a value matches a certain pattern. It turns code like this: | ||
loop as long as a value matches a certain pattern. It turns code like this: | ||
|
||
```rust | ||
# let option: Option<i32> = None; | ||
|
@@ -73,8 +73,9 @@ loop { | |
Into code like this: | ||
|
||
```rust | ||
# let option: Option<i32> = None; | ||
while let Some(x) = option { | ||
let v: vec![1, 3, 5, 7, 11, ]; | ||
let mut iter: v.iter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be =, not : on these two lines. But, why not show off something else than iterators since that's a good while let speciality? For example looping over the result of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let mut v = vec![1, 3, 5, 7, 11];
while let Some(x) = v.pop() {
println!("{}", x);
} |
||
while let Some(x) = iter.next() { | ||
println!("{}", x); | ||
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"no sugar" version of the while let over v.pop() looks like