This is the unpublished documentation of wasm-bindgen, the published documentation is available on the main Rust and WebAssembly documentation site . Features documented here may not be available in released versions of wasm-bindgen.

Boxed Number Slices: Box<[u8]>, Box<[i8]>, Box<[u16]>, Box<[i16]>, Box<[u32]>, Box<[i32]>, Box<[u64]>, Box<[i64]>, Box<[f32]>, and Box<[f64]>

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 TypedArray of the appropriate type (Int32Array, Uint8Array, etc...)

Note that the contents of the slice are copied into the JavaScript TypedArray from the Wasm linear memory when returning a boxed slice to JavaScript, and vice versa when receiving a JavaScript TypedArray as a boxed slice in Rust.

Example Rust Usage

# #![allow(unused_variables)]
#fn main() {
use wasm_bindgen::prelude::*;

pub fn take_boxed_number_slice_by_value(x: Box<[f64]>) {}

pub fn return_boxed_number_slice() -> Box<[u32]> {

pub fn take_option_boxed_number_slice(x: Option<Box<[u8]>>) {}

pub fn return_option_boxed_number_slice() -> Option<Box<[i32]>> {


Example JavaScript Usage

import {
} from './guide_supported_types_examples';

take_boxed_number_slice_by_value(new Uint8Array(100));

let x = return_boxed_number_slice();
console.log(x instanceof Uint32Array); // true

take_option_boxed_number_slice(new Int16Array(256));

let y = return_option_boxed_number_slice();
if (y == null) {
  // ...
} else {
  console.log(x instanceof Int32Array); // true