guide: add String example usage to supported types

This commit is contained in:
Nick Fitzgerald 2018-08-13 16:20:25 -07:00
parent fa72afe286
commit 74dc8874e1
4 changed files with 52 additions and 0 deletions

View File

@ -6,3 +6,4 @@ extern crate wasm_bindgen;
pub mod imported_types;
pub mod exported_types;
pub mod str;
pub mod string;

View File

@ -0,0 +1,17 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn take_string_by_value(x: String) {}
#[wasm_bindgen]
pub fn return_string() -> String {
"hello".into()
}
#[wasm_bindgen]
pub fn take_option_string(x: Option<String>) {}
#[wasm_bindgen]
pub fn return_option_string() -> Option<String> {
None
}

View File

@ -0,0 +1,22 @@
import {
take_string_by_value,
return_string,
take_option_string,
return_option_string,
} from './guide_supported_types_examples';
take_string_by_value('hello');
let s = return_string();
console.log(typeof s); // "string"
take_option_string(null);
take_option_string(undefined);
take_option_string('hello');
let t = return_option_string();
if (t == null) {
// ...
} else {
console.log(typeof s); // "string"
}

View File

@ -73,6 +73,18 @@ Copies the string's contents back and forth between the JavaScript
garbage-collected heap and the Wasm linear memory with `TextDecoder` and
`TextEncoder`
### Example Rust Usage
```rust
{{#include ../../../examples/guide-supported-types-examples/src/string.rs}}
```
### Example JavaScript Usage
```js
{{#include ../../../examples/guide-supported-types-examples/string.js}}
```
## `char`
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |