char
T parameter | &T parameter | &mut T parameter | T return value | Option<T> parameter | Option<T> return value | JavaScript representation |
---|---|---|---|---|---|---|
Yes | No | No | Yes | Yes | Yes | A JavaScript string value |
Since JavaScript doesn't have a character type, char
is represented as a JavaScript string with one Unicode code point.
Note: JavaScript strings uses UTF-16 encoding. This means that a single
char
may be represented by a string of length 1 or 2 in JavaScript, depending on the Unicode code point. SeeString.fromCodePoint
for more information.
When passed into Rust, the char
value of a JavaScript string is determined using codePointAt(0)
. If the JavaScript string is empty or starts with an unpaired surrogate, a runtime error will be thrown.
Note: For more information about unpaired surrogates, see the documentation for
str
.
Example Rust Usage
# #![allow(unused_variables)] #fn main() { use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn take_char_by_value(x: char) {} #[wasm_bindgen] pub fn return_char() -> char { '🚀' } #}
Example JavaScript Usage
import {
take_char_by_value,
return_char,
} from './guide_supported_types_examples';
take_char_by_value('a');
let c = return_char();
console.log(typeof c); // "string"