当尝试制作全屏 glfw window 时:不能将 `glfw` 借用为可变的,因为它也被借用为不可变的

When trying to make fullscreen glfw window: cannot borrow `glfw` as mutable because it is also borrowed as immutable

fn main() {
    let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();

                                // | Error occurs here
    let (mut window, events) = glfw.with_primary_monitor(|_, m| {
        glfw.create_window(300, 300, "Window",
            m.map_or(glfw::WindowMode::Windowed, |m| glfw::WindowMode::FullScreen(m)))
    }).expect("Failed to create GLFW window");
}

准确的错误是:

error[E0502]: cannot borrow `glfw` as mutable because it is also borrowed as immutable
 --> src/main.rs:5:32
  |
5 |       let (mut window, events) = glfw.with_primary_monitor(|_, m| {
  |                                  ^    -------------------- ------ immutable borrow occurs here
  |                                  |    |
  |  ________________________________|    immutable borrow later used by call
  | |
6 | |         glfw.create_window(300, 300, "Window",
  | |         ---- first borrow occurs due to use of `glfw` in closure
7 | |             m.map_or(glfw::WindowMode::Windowed, |m| glfw::WindowMode::FullScreen(m)))
8 | |     }).expect("Failed to create GLFW window");
  | |______^ mutable borrow occurs here

with_primary_monitor 中传递给闭包的第一个参数是 glfw 对象,您可以使用它来执行进一步的操作。使用该参数来使用 glfw,而不是使用来自外部范围的参数:

let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
let (mut window, events) = glfw.with_primary_monitor(|glfw, m| {
    glfw.create_window(300, 300, "Window",
        m.map_or(glfw::WindowMode::Windowed, |m| glfw::WindowMode::FullScreen(m)))
}).expect("Failed to create GLFW window");