mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-22 04:32:13 +00:00
This commit is a large-ish scale reorganization of our examples. The main goal here is to have a dedicated section of the guide for example, and all examples will be listed there. Each example's `README` is now just boilerplate pointing at the guide along with a blurb about how to run it. Some examples like `math` and `smorgasboard` have been deleted as they didn't really serve much purpose, and others like `closures` have been rewritten with `web-sys` instead of hand-bound bindings. Overall it's hoped that this puts us in a good and consistent state for our examples, with all of them being described in the guide, excerpts are in the guide, and they're all relatively idiomatically using `web-sys`.
26 lines
913 B
Rust
26 lines
913 B
Rust
extern crate wasm_bindgen;
|
|
extern crate web_sys;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
// Called by our JS entry point to run the example
|
|
#[wasm_bindgen]
|
|
pub fn run() -> Result<(), JsValue> {
|
|
// Use `web_sys`'s global `window` function to get a handle on the global
|
|
// window object.
|
|
let window = web_sys::window().expect("no global `window` exists");
|
|
let document = window.document().expect("should have a document on window");
|
|
let body = document.body().expect("document should have a body");
|
|
|
|
// Manufacture the element we're gonna append
|
|
let val = document.create_element("p")?;
|
|
val.set_inner_html("Hello from Rust!");
|
|
|
|
// Right now the class inheritance hierarchy of the DOM isn't super
|
|
// ergonomic, so we manually cast `val: Element` to `&Node` to call the
|
|
// `append_child` method.
|
|
AsRef::<web_sys::Node>::as_ref(&body).append_child(val.as_ref())?;
|
|
|
|
Ok(())
|
|
}
|