diff --git a/examples/guide-supported-types-examples/src/lib.rs b/examples/guide-supported-types-examples/src/lib.rs index afe92e4bc..f0f83f8e8 100755 --- a/examples/guide-supported-types-examples/src/lib.rs +++ b/examples/guide-supported-types-examples/src/lib.rs @@ -5,3 +5,4 @@ extern crate wasm_bindgen; pub mod imported_types; pub mod exported_types; +pub mod str; diff --git a/examples/guide-supported-types-examples/src/str.rs b/examples/guide-supported-types-examples/src/str.rs new file mode 100644 index 000000000..a78787336 --- /dev/null +++ b/examples/guide-supported-types-examples/src/str.rs @@ -0,0 +1,4 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn take_str_by_shared_ref(x: &str) {} diff --git a/examples/guide-supported-types-examples/str.js b/examples/guide-supported-types-examples/str.js new file mode 100644 index 000000000..4909b6f66 --- /dev/null +++ b/examples/guide-supported-types-examples/str.js @@ -0,0 +1,5 @@ +import { + take_str_by_shared_ref, +} from './guide_supported_types_examples'; + +take_str_by_shared_ref('hello'); diff --git a/guide/src/reference/types.md b/guide/src/reference/types.md index 9bcf37b27..64659ca01 100644 --- a/guide/src/reference/types.md +++ b/guide/src/reference/types.md @@ -44,13 +44,25 @@ JavaScript. | `T` parameter | `&T` parameter | `&mut T` parameter | `T` return value | `Option` parameter | `Option` 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` parameter | `Option` return value | JavaScript representation |