简体   繁体   English

let chain causing rust-analyzer 抱怨 Rust 1.66 中的功能不稳定,这不是最近才合并到稳定的吗?

[英]While let chain causing rust-analyzer to complain about the feature being unstable in Rust 1.66, wasn't this just merged into stable recently?

    while let Some(peek_ch) = chars.peek() && peek_ch.is_whitespace() {
      chars.next();
    }

The above code is causing rust to complain with上面的代码导致 rust 抱怨

`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?我的理解是 if-let 和 while-let 链接是否稳定? 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?此外,从这个错误和 github 问题我无法确定要启用什么不稳定的功能来允许这个,你通常如何确定?

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问题是你还不允许在while let中使用&&因为不幸的是必须恢复稳定的合并

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:所以这适用于夜间 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();
    }
}

Playground link 游乐场链接

Or you can work around it with one of PeterHalls stable solutions.或者您可以使用 PeterHall 的稳定解决方案之一解决它。

The problem is the && , not the while let .问题是&& ,而不是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 .如所写,您正在尝试匹配表达式chars.peek() && peek_ch.is_whitespace()的结果,但这没有意义,因为peek返回Option<T>is_whitespace返回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.该错误消息有点误导,因为它认为您正在尝试使用if let chains ,这是一种不稳定的功能,可让您匹配多种模式。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM