如何匹配具有多个字段的选项?我有以下代码:
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 library。
答案 0 :(得分:6)
let window = match opt_windowrecv {
Some((window, _)) => window,
None => return
};
应该有效。 Some
的解构应该是一个元组,而不是两个参数。