Skip to content

Getting Started (Rust)

Madita Antonia Plogsties edited this page May 22, 2025 · 3 revisions

Working on Rust

For this project, we don't have a backend like a traditional server with self-coded functionality running in the background. Instead, we use Rust code for the algorithmic logic, located inside the rust/ folder.

If you see any references to the "Backend", it refers to this Rust codebase. Each algorithm lives in its own folder and represents a Rust crate (crates are like packages in other languages).

Inside each crate, you'll find:

  • src/ — the Rust source code
  • pkg/ — the compiled wasm output, which is used by the frontend (Node.js)

🔧 Setup and Compilation

Prerequisites

Make sure you have the following tools installed:

To install wasm-pack:

cargo install wasm-pack

To install the nightly Rust toolchain (required for formatting):

rustup install nightly

How to Compile

To compile a crate to WebAssembly, go to the crate folder with your changes and run:

wasm-pack build --target web

💡 This creates the compiled wasm files in the pkg/ folder, which are then imported by the frontend.

You must recompile every time you make changes to the Rust code.

We commit the pkg/ folders to the repository so that team members who don't work with Rust don't need to install it or run the build process themselves.


How do I format Rust code?

Use the following command to format Rust code:

cargo +nightly fmt --all

Or use the shortcut via Yarn:

yarn format:algo

❗️You need to format the Rust code, otherwise the pipeline will fail!


Clippy (Linting)

When working in Rust you can use Clippy, which provides you with a collection of lints to catch common mistakes and improve your code quality. In your working directory, use cargo clippy.

What is defined in the Cargo.toml?

A lot. But most of the stuff we don't care about. A complete guide can be found here: https://doc.rust-lang.org/cargo/reference/manifest.html. In the following we will focus on those parts that are actually relevant to us.

[package]

We usually have four lines in this section. The first defines the name of the crate and is the most relevant one to us. Then we define the version of the crate which is pointless since we will never publish it anyways, and to prevent that from happening on accident, we set publish = false. The last line is edition = 2021, which makes sure we get all of the goddies of the (currently) newest Rust edition. Rust editions are a way to make small changes to the Rust language and syntax without breaking backwards-compatibility.

[lib]

crate-type = ["cdylib"]

Should be choosen when compiling Rust to wasm package

[dependencies]

List of used libraries with version number.

console_error_panic_hook
  • when Rust throws an error this is logged to the js console (you must call console_error_panic_hook::set_once(); at program start in your code)
WASM Packages

gloo-utils

wasm-bindgen

  • They are used to provide and bind an interface which is usable by the js code.

More information can be found here.

Clone this wiki locally