Answer#
winit is the crate that creates windows and delivers events on Windows, macOS and Linux. Several toolkits (egui's eframe, Slint's winit backend, and others) are built on it. You use it directly when you draw the window yourself, for example with a graphics API.
In winit 0.30 you write a struct that implements ApplicationHandler. The event loop calls its methods; you create the window in resumed, the moment the system says it is ready.
[dependencies]
winit = "0.30"use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};
#[derive(Default)]
struct App {
window: Option<Window>,
}
impl ApplicationHandler for App {
// Windows must be created here: the system says it is now ready for them.
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let attrs = Window::default_attributes().with_title("winit window");
self.window = Some(event_loop.create_window(attrs).expect("cannot create the window"));
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::Resized(size) => println!("resized to {}x{} physical pixels", size.width, size.height),
WindowEvent::ScaleFactorChanged { scale_factor, .. } => println!("scale factor is now {scale_factor}"),
WindowEvent::DroppedFile(path) => println!("dropped: {}", path.display()),
WindowEvent::RedrawRequested => { /* draw the frame here */ }
_ => {}
}
}
}
fn main() -> Result<(), winit::error::EventLoopError> {
let event_loop = EventLoop::new()?;
event_loop.set_control_flow(ControlFlow::Wait); // sleep until something happens
event_loop.run_app(&mut App::default())
}This passed cargo check with winit 0.30.13 on Windows 11. It was not run.
Notes:
ControlFlow::Waitmakes the loop sleep until an event arrives;ControlFlow::Pollruns it continuously, which uses a CPU core.- A window shows nothing by itself. To draw you add a renderer (a graphics API, or a software buffer crate); winit only gives you the window and the events.
- Sizes in
Resizedare in physical pixels. See the thread on high-DPI for the difference between logical and physical sizes.
Common mistakes#
- Following an example from before winit 0.30, which creates the window before the loop starts and passes a closure to
run. In 0.30 theApplicationHandlerstyle shown here is the one to use; check the version in yourCargo.toml. - Treating
resumedas "called once". Guard withif self.window.is_none()so a second call does not create a second window. - Doing slow work in
window_event; it blocks the loop.
Related questions#
Do I need winit if I use egui or Slint?#
No. Those toolkits use it for you.
Sources#
- winit documentation: https://docs.rs/winit/0.30.13/winit/ (read 2026-12-05)
- winit repository: https://github.com/rust-windowing/winit (read 2026-12-05)