What is the syntax for an if-let statement?

拥有回忆 提交于 2021-02-05 11:21:07

问题


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 {
  glutin::Event::WindowEvent { event, .. } => match event {
      glutin::WindowEvent::Closed => return glutin::ControlFlow::Break,
      glutin::WindowEvent::Resized(w, h) => gl_window.resize(w, h),
      _ => (),
  },
  _ => ()
}

This was my attempt to restructure it:

if let _ = glutin::Event::WindowEvent { event, .. } {
    match event {
       glutin::WindowEvent::Closed => return glutin::ControlFlow::Break,
       glutin::WindowEvent::Resized(w, h) => gl_window.resize(w, h),
       _ => (),
   }
}

Oops, that's a syntax error. What would be the correct way to clear the linter warning?

After looking at the code more closely, I realized that I don't understand the syntax. glutin::Event::WindowEvent { event, .. } looks like the syntax for creating a new instance of WindowEvent but how can that be allowed inside a match statement?

Also, what does the .. mean? I'm familiar with ..Default::default(), but not the double dot by itself.


回答1:


The syntax that eludes you is called destructuring.

This pattern allows to match certain fields in a struct, enum, or tuple. You therefore cannot just use if let with the destructuring on the right side of the binding.

The code you want is probably:

if let glutin::Event::WindowEvent { event, .. } = event {
  match event {
      glutin::WindowEvent::Closed => return glutin::ControlFlow::Break,
      glutin::WindowEvent::Resized(w, h) => gl_window.resize(w, h),
      _ => (),
  }
}

There is a possible confusion between the right hand event variable and the one extracted from the pattern. The use of event in the destructuring is made mandatory because it needs to use struct fields by name.




回答2:


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 {
    // ...
}


来源:https://stackoverflow.com/questions/46981148/what-is-the-syntax-for-an-if-let-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!