bool
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 boolean value |
Note: Only JavaScript
Boolean
values (true
orfalse
) are supported when calling into Rust. If you want to pass truthy or falsy values to Rust, convert them to a boolean usingBoolean(value)
first.If you are using TypeScript, you don't have to worry about this, as TypeScript will emit a compiler error if you try to pass a non-
boolean
value.
Example Rust Usage
#![allow(unused_variables)]
fn main() {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn take_bool_by_value(x: bool) {}
#[wasm_bindgen]
pub fn return_bool() -> bool {
true
}
#[wasm_bindgen]
pub fn take_option_bool(x: Option<bool>) {}
#[wasm_bindgen]
pub fn return_option_bool() -> Option<bool> {
Some(false)
}
}
Example JavaScript Usage
import {
take_char_by_value,
return_char,
take_option_bool,
return_option_bool,
} from './guide_supported_types_examples';
take_bool_by_value(true);
let b = return_bool();
console.log(typeof b); // "boolean"
take_option_bool(null);
take_option_bool(undefined);
take_option_bool(true);
let c = return_option_bool();
if (c == null) {
// ...
} else {
console.log(typeof c); // "boolean"
}