Using Rust with Chicory
Compile Rust to Wasm
Compiling a Rust library to Wasm is easy and can be performed using standard rustc
options:
rustc --target=wasm32-unknown-unknown --crate-type=cdylib
when you need to add support for wasi preview 1(typically when using CLIs) you can use:
rustc --target=wasm32-wasi --crate-type=bin
Using in Chicory
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.runtime.Instance;
var instance = Instance.builder(Parser.parse(new File("count_vowels.rs.wasm"))).build();
var alloc = instance.export("alloc");
var dealloc = instance.export("dealloc");
var countVowels = instance.export("count_vowels");
var memory = instance.memory();
var message = "Hello, World!";
var len = message.getBytes().length;
int ptr = (int) alloc.apply(len)[0];
memory.writeString(ptr, message);
var result = countVowels.apply(ptr, len)[0];
System.out.println(result);