Docs
Concepts
Typed Array

TypedArray

TypedArray describes an array-like view of an underlying binary data buffer. Using TypedArray allows you to share data between Node.js and Rust without copy or move data underlying.

Buffer

Buffer is a subclass of JavaScript's Uint8Array. It is often used to share data between Node.js and Rust.

Buffer could be created with Vec<u8>, if you created Buffer in this way, the ownership of the Vec<8> will be transferred into the v8, and the Vec<u8> will be dropped when v8 GC the Buffer.

use napi::bindgen_prelude::*;
use napi_derive::napi;

#[napi]
pub fn create_buffer() -> Buffer {
  vec![0, 1, 2].into()
}
💡

The underlying Vec<u8> will not be moved or copied in this way.