If you want to combine the Web Fundamentals & Modern UX of Remix together with the Reliability, Performance & Efficiency of Rust, you can use functions built with Rust on your server. Useful for intensive computations such as on the fly machine learning tasks, fibonacci etc.
Open this example on CodeSandbox:
This example uses Rust compiled to WASM.
If you don't have Rust installed on your computer the first thing to do is to get this set up
Installing Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Here we use wasm-pack
to comile our Rust code down to WASM
Installing wasm-pack:
cargo install wasm-pack
In this example, the Rust library is already built with the associated code. But if you wanted to set up your own library you could do so by running the following command:
cargo new --lib <library-name>
Then build the library using:
cd <library-name>
wasm-pack build --target nodejs
After succesfully bulding the library you can add this to your dependencies by running inside your remix project:
npm install ./<library-name>/pkg
This will add the dependency to your package.json
file.
...
"@remix-run/serve": "1.1.3",
"<library-name>": "file:<library-name>/pkg"
...
To prevent remix from including the rust-functions library in the client build we can re-export the functions using a .server.ts
file, e.g. rust.server.ts