gpt4 book ai didi

rust - 如何将闭包转换为 js_sys::Function?

转载 作者:行者123 更新时间:2023-12-03 11:46:05 25 4
gpt4 key购买 nike

如何转换本地 closure进入 js_sys::Funtion ?

我想做这样的事情:

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()));

最佳答案

我找到了这个。

https://rustwasm.github.io/wasm-bindgen/reference/passing-rust-closures-to-js.html

它像是:

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!

关于rust - 如何将闭包转换为 js_sys::Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60054963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com