What is the syntax for an if-let statement?

后端 未结 2 1241
难免孤独
难免孤独 2021-01-28 02:46

I encountered this snippet in some example code. It works fine, but I got a linter error saying that it should be structured as an if-let statement.

match event         


        
2条回答
  •  孤独总比滥情好
    2021-01-28 03:32

    Directly quoting from The Book, second edition:

    The if let syntax lets you combine if and let into a less verbose way to handle values that match one pattern and ignore the rest.

    It also provides this example:

    if let Some(3) = some_u8_value {
        println!("three");
    }
    

    The correct syntax is if let «pattern» = «expression» { ... }, and not the opposite written in the question.

    if let glutin::Event::WindowEvent { event, .. } = event {
        // ...
    }
    

提交回复
热议问题