let canvas = document.get_element_by_id("canvas").unwrap();
let e: web_sys::HtmlElement = canvas.dyn_into().unwrap();
let f = || {};
e.set_onresize(Some(&f.into()));
let f = Closure::wrap(Box::new(move || { /* whatever */}) as Box<dyn FnMut()>);
e.set_onresize(Some(f.as_ref().unchecked_ref()));
f.forget(); // It is not good practice, just for simplification!
2条答案
按热度按时间r7xajy2e1#
我找到了这个。
https://rustwasm.github.io/wasm-bindgen/reference/passing-rust-closures-to-js.html
它就像:
3ks5zfa02#
答案也可以在
wasm_bindgen::closure::Closure
页面上找到。