Skip to content

Latest commit

 

History

History

rust

Example using Rust

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.

Preview

Open this example on CodeSandbox:

Open in CodeSandbox

Example

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 compile 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 successfully building 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": "2.9.2",
    "<library-name>": "file:<library-name>/pkg"
    ...

Notes:

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

Related Links

Rust

Wasm-pack

Tensorflow & WebAssembly