expression

Return owned bytes as a typed array

Returning a boxed numeric slice through wasm-bindgen copies its contents into a JavaScript typed array. The host can keep the returned array without managing a raw guest pointer. Copying can cost time and memory for large payloads; do not assume zero-copy. Expected result (NOT RUN): A Uint8Array containing [2,4,6]; changing the returned array does not change a subsequent result. Common mistake: Treating a copied array as a live borrowed Wasm memory view. Static Rust source module for the accompanying Cargo lab; not a standalone executable or ByteMind exercise.

Formal Syntax & Structure

rust syntaxrust
#[wasm_bindgen] pub fn sample_bytes() -> Box<[u8]>

Working Example

Example Snippetrust
use wasm_bindgen::prelude::*;#[wasm_bindgen]pub fn sample_bytes() -> Box<[u8]> { vec![2, 4, 6].into_boxed_slice() }
Rust Reference