rust-module
A page that calls a Rust module (modules/maths), built to WebAssembly with types, and an about page built on rung 0 (decisions 0075 and 0078). Needs the wasm32-unknown-unknown target and wasm-bindgen-cli 0.2.130.
Run it
cargo run -p duolith-cli -- dev examples/rust-module # live, reloads on save
cargo run -p duolith-cli -- build examples/rust-module # static output in examples/rust-module/dist
The folder on the web: examples/rust-module.
Features it shows
| Feature | Where | Screenshot |
|---|---|---|
| Rung 0 tables: files whose expressions are all constants build as a template and a table of constants, with none of their script (decision 0075) | styling (5 of 6 pages), rust-module pages/about.duo, social pages/about.duo, navigation-kit library and settings/about, the kit and inputs-and-media index pages | duolith build examples/styling --report |
| Rust modules: a page calls Rust, built to WebAssembly, with types; Swift and Kotlin bindings from UniFFI (decision 0078) | rust-module pages/index.duo, modules/maths | duolith dev examples/rust-module; pnpm --filter @duolith/modules bindings:native examples/rust-module/modules/maths |
Source
modules/maths/index.d.ts
// Generated by `duolith build` from this module's Rust code. Do not edit.
/* tslint:disable */
/* eslint-disable */
/**
* The `n`th Fibonacci number, counting from `fibonacci(0) == 0`. Past
* `n == 78` the answer is larger than a JavaScript number holds exactly, and
* past `n == 1476` it is infinity. `n` is a JavaScript number, so one that is
* negative or not whole gives NaN rather than wrapping round to a huge count.
*/
export function fibonacci(n: number): number;
/**
* `text` as a lowercase address segment: letters and digits kept, every run
* of anything else turned into one `-`.
*/
export function slugify(text: string): string;pages/about.duo
---
const steps = [
"duolith build finds modules/maths/Cargo.toml",
"cargo compiles it to WebAssembly",
"wasm-bindgen writes its loader and types",
"the page's import points at the loader",
];
const ladder = "This page has no state, so it is built on rung 0: a table, with no script.";
---
<screen title="About" class="p-6 gap-4 bg-canvas">
<text class="text-title text-ink">How it is built</text>
<list each={steps} as="step" index="i" class="gap-2">
<text class="text-body text-ink">{i + 1}. {step}</text>
</list>
<text class="text-body text-muted">{ladder}</text>
<button class="text-primary" href="/">Back to the module</button>
</screen>pages/index.duo
---
import { derived, state } from "@duolith/client";
import { fibonacci, slugify } from "../modules/maths";
let n = state(10);
let title = state("Hello from Rust");
let answer = derived(() => fibonacci(n));
let slug = derived(() => slugify(title));
function more() {
n = Math.min(n + 1, 78);
}
function fewer() {
n = Math.max(n - 1, 0);
}
---
<screen title="Rust module" class="p-6 gap-6 bg-canvas">
<text class="text-title text-ink">Calling Rust from a page</text>
<text class="text-body text-muted">
These functions are written in Rust (modules/maths), built to WebAssembly by duolith build, and
called from this page's script with their types.
</text>
<view class="gap-3 p-4 rounded-lg bg-surface">
<text class="text-heading text-ink">fibonacci({n}) = {answer}</text>
<view class="row gap-3">
<button
class="px-4 py-2 rounded-md bg-primary text-on-primary"
on:press={fewer}
label="Fewer"
>
−
</button>
<button class="px-4 py-2 rounded-md bg-primary text-on-primary" on:press={more} label="More">
+
</button>
</view>
</view>
<view class="gap-3 p-4 rounded-lg bg-surface">
<text class="text-heading text-ink">slugify</text>
<input class="px-3 py-2 rounded-md bg-canvas text-ink" bind:value={title} label="Title" />
<text class="text-body text-ink">/posts/{slug}</text>
</view>
<button class="text-primary" href="/about">How this page was built</button>
</screen>