Files
wazero/examples/allocation/rust
Crypt Keeper 03bfa31928 Makes all examples and docs use Runtime.Close (#537)
This removes tedium in our examples and docs by using `Runtime.Close`
instead of tracking everything. Internal tests still track too much, but
anyway at least this stops suggesting others should do it.

This also changes our examples to use log.PanicXX so that the line
number goes into the console output.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-05-10 12:08:25 +08:00
..

Rust allocation example

This example shows how to pass strings in and out of a Wasm function defined in Rust, built with cargo build --release --target wasm32-unknown-unknown

Ex.

$ go run greet.go wazero
Hello, wazero!

Under the covers, lib.rs does a few things of interest:

  • Uses a WebAssembly-tuned memory allocator: wee_alloc.
  • Exports wrapper functions to allocate and deallocate memory.
  • Uses &str instead of CString (NUL-terminated strings).
  • Uses std::mem::forget to prevent Rust from eagerly freeing pointers returned.

Note: We chose to not use CString because it keeps the example similar to how you would track memory for arbitrary blobs. We also watched function signatures carefully as Rust compiles different WebAssembly signatures depending on the input type. All of this is Rust-specific, and wazero isn't a Rust project, but we hope this gets you started. For next steps, consider reading the Rust and WebAssembly book.