简体   繁体   English

如何匹配具有多个字段的期权?

[英]How to match for an Option with multiple fields?

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 此模式有2个字段,但相应的变体有1个字段

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? 我是在做错什么,还是Rust编译器中的错误? I am using the glfw-rs library . 我正在使用glfw-rs库

let window = match opt_windowrecv {
    Some((window, _)) => window,
    None            => return
};

Should work. 应该管用。 The destructuring of Some should be a tuple, not 2 arguments. Some的破坏应该是一个元组,而不是2个参数。

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

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