mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-27 03:55:20 +03:00
guide: add String
example usage to supported types
This commit is contained in:
parent
fa72afe286
commit
74dc8874e1
@ -6,3 +6,4 @@ extern crate wasm_bindgen;
|
||||
pub mod imported_types;
|
||||
pub mod exported_types;
|
||||
pub mod str;
|
||||
pub mod string;
|
||||
|
17
examples/guide-supported-types-examples/src/string.rs
Normal file
17
examples/guide-supported-types-examples/src/string.rs
Normal 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
|
||||
}
|
22
examples/guide-supported-types-examples/string.js
Normal file
22
examples/guide-supported-types-examples/string.js
Normal 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"
|
||||
}
|
@ -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 |
|
||||
|
Loading…
Reference in New Issue
Block a user