I would like to compile a Rust program/project to Wasm for use within my Python application using python-ext-wasm. The existing tutorials assume that it's for the web and suggest wasm-pack
. Is there another way of just compiling Rust to Wasm without JavaScript bindings?
For example, if I have a Rust program (myproject/math.rs
).
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
How do I convert that into a wasm
file without webpack
?
How do I take an entire project (with it's library dependencies) and compile all of them to Wasm?
Wasm is 1.15-1.67 times faster than JavaScript on Google Chrome on a desktop. Wasm is 1.95-11.71 times faster than JavaScript on Firefox on a desktop.
You can compile WebAssembly directly with cargo build --target wasm32-unknown-unknown
. This is essentially what other tooling like wasm-pack
and wasm-bindgen
are built around, and if you don't want that (e.g. if you're not targeting JavaScript) you can just use that to compile directly to WebAssembly.
Some caveats though:
extern
functions. This means that there's only a limited number of types that can be used, mostly primitive types (integers, floats, booleans and pointers). You won't be able to pass complex types unless you're using an additional layer of abstraction on top (which is what wasm-bindgen
does).wasm32-unknown-unknown
target. If your WebAssembly host supports WASI (WebAssembly System Interface), you can use the wasm32-wasi
target instead to compile a module with WASI support, which supports much more of the standard library.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With