mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-09 19:07:30 +02:00
24 lines
593 B
Markdown
24 lines
593 B
Markdown
|
# Expose a method
|
||
|
|
||
|
The first thing you will want to do with WebAssembly is expose your methods to Javascript.
|
||
|
This is straightforward using the `#[wasm_bindgen]` procedural macro.
|
||
|
|
||
|
```rust
|
||
|
use wasm_bindgen::prelude::*;
|
||
|
|
||
|
#[wasm_bindgen]
|
||
|
pub fn set_panic_hook() {
|
||
|
// Generates better error messages if our code ever panics.
|
||
|
//
|
||
|
// For more details see
|
||
|
// https://github.com/rustwasm/console_error_panic_hook#readme
|
||
|
console_error_panic_hook::set_once();
|
||
|
}
|
||
|
|
||
|
// Exposes the `add` method from the previous slide
|
||
|
#[wasm_bindgen]
|
||
|
pub fn add(a: i32, b: i32) -> i32 {
|
||
|
a + b
|
||
|
}
|
||
|
```
|