如何匹配具有多个字段的选项?(How to match for an Option with multiple fields?)
如何匹配具有多个字段的选项? 我有以下代码:
let opt_windowrecv = glfw_context.create_window(1280, 720, "Hello World!", glfw::Windowed); // returns Option<(Window, Receiver<(f64, WindowEvent)>)> let window = match opt_windowrecv { // Does not compile Some(window, _) => window, None => return };它抱怨说:
这个模式有2个字段,但相应的变量有1个字段
然而,使用此解决方法确实有效:
let opt_windowrecv = glfw_context.create_window(1280, 720, "Hello World!", glfw::Windowed); // returns Option<(Window, Receiver<(f64, WindowEvent)>)> let windowrecv = match opt_windowrecv { Some(windowrecv) => windowrecv, None => return }; let (window, _) = windowrecv;我做错了什么,或者这是Rust编译器中的错误? 我正在使用glfw-rs库 。
How do I match for an Option with multiple fields? I have the following code:
let opt_windowrecv = glfw_context.create_window(1280, 720, "Hello World!", glfw::Windowed); // returns Option<(Window, Receiver<(f64, WindowEvent)>)> let window = match opt_windowrecv { // Does not compile Some(window, _) => window, None => return };It complains saying:
this pattern has 2 fields, but the corresponding variant has 1 field
Using this workaround does however work:
let opt_windowrecv = glfw_context.create_window(1280, 720, "Hello World!", glfw::Windowed); // returns Option<(Window, Receiver<(f64, WindowEvent)>)> let windowrecv = match opt_windowrecv { Some(windowrecv) => windowrecv, None => return }; let (window, _) = windowrecv;Am I doing something wrong, or is this a bug in the Rust compiler? I am using the glfw-rs library.
最满意答案
let window = match opt_windowrecv { Some((window, _)) => window, None => return };应该管用。 Some的解构应该是一个元组,而不是2个参数。
let window = match opt_windowrecv { Some((window, _)) => window, None => return };Should work. The destructuring of Some should be a tuple, not 2 arguments.
更多推荐
发布评论