Exported struct Whatever Rust Types

T parameter &T parameter &mut T parameter T return value Option<T> parameter Option<T> return value JavaScript representation
Yes Yes Yes Yes No No Instances of a wasm-bindgen-generated JavaScript class Whatever { ... }

Example Rust Usage


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

#[wasm_bindgen]
pub struct ExportedRustType {
    inner: u32,
}

#[wasm_bindgen]
pub fn exported_type_by_value(x: ExportedRustType) {}

#[wasm_bindgen]
pub fn exported_type_by_shared_ref(x: &ExportedRustType) {}

#[wasm_bindgen]
pub fn exported_type_by_exclusive_ref(x: &mut ExportedRustType) {}

#[wasm_bindgen]
pub fn return_exported_type() -> ExportedRustType {
    unimplemented!()
}

#}

Example JavaScript Usage

import {
  ExportedRustType,
  exported_type_by_value,
  exported_type_by_shared_ref,
  exported_type_by_exclusive_ref,
  return_exported_type,
} from './guide_supported_types_examples';

let rustThing = return_exported_type();
console.log(rustThing instanceof ExportedRustType); // true

exported_type_by_value(rustThing);
exported_type_by_shared_ref(rustThing);
exported_type_by_exclusive_ref(rustThing);