Reputation: 1368
while let Some(peek_ch) = chars.peek() && peek_ch.is_whitespace() {
chars.next();
}
The above code is causing rust to complain with
`let` expressions in this position are unstable
see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
My understanding was if-let and while-let chaining was stabilized? Also, from this error and github issue I can't determine what unstable feature to enable to allow this, how do you normally determine that?
Upvotes: 3
Views: 670
Reputation: 27313
The problem is that you're not allowed to use &&
with while let
yet because unfortunately the merge of the stabilization had to be reverted
If you use a nightly compiler it will tell you what feature you have to enable to make it work:
> cargo +nightly run
...
error[E0658]: `let` expressions in this position are unstable
--> src/main.rs:4:11
|
4 | while let Some(peek_ch) = chars.peek() && peek_ch.is_whitespace() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
= help: add `#![feature(let_chains)]` to the crate attributes to enable
So this works with nightly rust:
#![feature(let_chains)]
fn main() {
let mut chars = "hello".chars().peekable();
while let Some(peek_ch) = chars.peek() && peek_ch.is_whitespace() {
chars.next();
}
}
Or you can work around it with one of PeterHalls stable solutions.
Upvotes: 3
Reputation: 58735
The problem is the &&
, not the while let
. As written, you are trying to match the result of the expression chars.peek() && peek_ch.is_whitespace()
, but this doesn't make sense because peek
returns an Option<T>
while is_whitespace
returns a bool
.
The error message is a little bit misleading because it thinks you are trying to use if let
chains, an unstable feature which allows you to match on multiple patterns.
You can rewrite this with:
while let Some(peek_ch) = chars.peek() {
if peek_ch.is_whitespace() {
chars.next();
} else {
break;
}
}
Or
while let Some(peek_ch) = chars.peek().filter(|c| c.is_whitespace()) {
chars.next();
}
Upvotes: 2