guide: Add str examples to supported types section

This commit is contained in:
Nick Fitzgerald 2018-08-13 16:12:58 -07:00
parent 8e19645006
commit fa72afe286
4 changed files with 23 additions and 1 deletions

View File

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

View File

@ -0,0 +1,4 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn take_str_by_shared_ref(x: &str) {}

View File

@ -0,0 +1,5 @@
import {
take_str_by_shared_ref,
} from './guide_supported_types_examples';
take_str_by_shared_ref('hello');

View File

@ -44,13 +44,25 @@ JavaScript.
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| No | Yes | No | Yes | Yes | No | JavaScript string value |
| No | Yes | No | No | No | No | JavaScript string value |
Copies the string's contents back and forth between the JavaScript
garbage-collected heap and the Wasm linear memory with `TextDecoder` and
`TextEncoder`. If you don't want to perform this copy, and would rather work
with handles to JavaScript string values, use the `js_sys::JsString` type.
### Example Rust Usage
```rust
{{#include ../../../examples/guide-supported-types-examples/src/str.rs}}
```
### Example JavaScript Usage
```js
{{#include ../../../examples/guide-supported-types-examples/str.js}}
```
## `String`
| `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option<T>` parameter | `Option<T>` return value | JavaScript representation |