| 1 |
# Working with the `char` type |
| 2 |
|
| 3 |
[View full source code][code] or [view the compiled example online][online] |
| 4 |
|
| 5 |
[online]: https://rustwasm.github.io/wasm-bindgen/exbuild/char/ |
| 6 |
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/char |
| 7 |
|
| 8 |
The `#[wasm_bindgen]` macro will convert the rust `char` type to a single |
| 9 |
code-point js `string`, and this example shows how to work with this. |
| 10 |
|
| 11 |
Opening this example should display a single counter with a random character |
| 12 |
for it's `key` and 0 for its `count`. You can click the `+` button to increase a |
| 13 |
counter's count. By clicking on the "add counter" button you should see a new |
| 14 |
counter added to the list with a different random character for it's `key`. |
| 15 |
|
| 16 |
Under the hood javascript is choosing a random character from an Array of |
| 17 |
characters and passing that to the rust Counter struct's constructor so the |
| 18 |
character you are seeing on the page has made the full round trip from js to |
| 19 |
rust and back to js. |
| 20 |
|
| 21 |
## `src/lib.rs` |
| 22 |
|
| 23 |
```rust |
| 24 |
{{#include ../../../examples/char/src/lib.rs}} |
| 25 |
``` |
| 26 |
|
| 27 |
## `index.js` |
| 28 |
|
| 29 |
```js |
| 30 |
{{#include ../../../examples/char/index.js}} |
| 31 |
``` |