Ownership in event loop closures. #2150
-
I'm slowly learning Rust and loving it, but confused about this pattern in the wgpu examples: // Have the closure take ownership of the resources.
// `event_loop.run` never returns, therefore we must do this to ensure
// the resources are properly cleaned up.
let _ = (&instance, &adapter, &shader, &pipeline_layout); When I remove this codeline, the compiler does not complain and there are no run time errors. These closures are annotated with the Also, I did the following experiment and observed that "exit" showed up in my console, even though I did not add struct Foo;
impl Drop for Foo { fn drop(&mut self) { println!("exit"); } }
impl Foo { fn hello(&self) { println!("hello"); } }
let foo = Foo {};
event_loop.run(move |event, _, control_flow| {
// Have the closure take ownership of the resources.
// `event_loop.run` never returns, therefore we must do this to ensure
// the resources are properly cleaned up.
let _ = (&instance, &adapter, &shader, &pipeline_layout);
foo.hello();
...
}
} Apologies for the dumb question! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Interesting question! So the reason those are on the capture list are because they are only use in initialization, inside the loop they are never used, so have no reason to be captured. We want them to be captured to make sure they are dropped, which is why we capture but immediately discard them. |
Beta Was this translation helpful? Give feedback.
Interesting question!
So the reason those are on the capture list are because they are only use in initialization, inside the loop they are never used, so have no reason to be captured. We want them to be captured to make sure they are dropped, which is why we capture but immediately discard them.