Я пытаюсь изучить WASM с помощью Rust, и я использую библиотеку web_sys
.
Страница, на которой я работаю, имеет ввод с id
, player_name_button
и кнопка с именем игрока После этого урока Закрытия , когда я пытаюсь поставить поведение над нажатием кнопки, я получаю ошибку компилятора:
|
84 | let a = Closure::wrap(Box::new(move || {
| ___________________________________^
85 | | name_input_value = value(&mut Some(player_name_input));
86 | | if name_input_value == String::new() {
87 | | alert("Por favor informe o nome!");
... |
93 | | }
94 | | }) as Box<dyn FnMut()>);
| |______________^ expected an `FnMut<()>` closure, found `[closure@src\lib.rs:84:44: 94:14 player_name_input:web_sys::features::gen_Element::Element, name_input_value:std::string::String, player_storage:player::player::PlayerSotorage]`
Вот код:
let player_name_input = document
.get_element_by_id("player_name_input")
.expect("should have #num-clicks on the page");
let mut name_input_value = String::new();
//
let a = Closure::wrap(Box::new(move || {
name_input_value = value(&mut Some(player_name_input));
if name_input_value == String::new() {
alert("Please informe the name!");
} else {
let mut p = Player {
name: name_input_value,
};
player_storage.insert(p);
}
}) as Box<dyn FnMut()>);
document
.get_element_by_id("player_name_button")
.expect("should have #player_name_button on the page")
.dyn_ref::<HtmlElement>()
.expect("#player_name_button be an `HtmlElement`")
.set_onclick(Some(a.as_ref().unchecked_ref()));
// See comments in `setup_clock` above for why we use `a.forget()`.
a.forget();
Вот этот автомобиль go .томл
[lib]
crate-type = ["cdylib", "rlib"]
[build-dependencies]
askama = "0.7.2"
[dependencies]
wasm-bindgen = "0.2.60"
js-sys = "0.3.37"
askama = "0.7.2"
console_error_panic_hook = "0.1.5"
[dependencies.web-sys]
version = "0.3.5"
features = [
'console',
'CssStyleDeclaration',
'Document',
'DomStringMap',
'DomTokenList',
'Element',
'Event',
'EventTarget',
'HtmlBodyElement',
'HtmlElement',
'HtmlInputElement',
'KeyboardEvent',
'Location',
'Node',
'NodeList',
'Storage',
'Window',
]
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.2"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"