winit initialize in rust

To initialize a winit window and event loop in Rust, you can use the following code:

main.rs
use winit::{event_loop::EventLoop, window::Window};

fn main() {
    let event_loop = EventLoop::new();
    let window = Window::new(&event_loop).unwrap();

    // ...
    
    event_loop.run(move |event, _, control_flow| {
        // Handle events here...

        // Set control flow to Wait to keep the event loop running
        *control_flow = winit::event_loop::ControlFlow::Wait;
    });
}
397 chars
16 lines

This code creates a new EventLoop and Window instance, and then starts the event loop by calling the run method.

The move keyword is used to move ownership of the event loop and control flow into the closure provided to run.

Within the closure, events can be handled and the control flow can be set to Wait to keep the event loop running.

gistlibby LogSnag