如何匹配具有多个字段的选项?(How to match for an Option with multiple fields?)

编程入门 行业动态 更新时间:2024-10-05 15:29:43
如何匹配具有多个字段的选项?(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.

更多推荐

本文发布于:2023-08-07 13:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字段   选项   match   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!