TechBuzzPro

Rust WebAssembly Full Stack: Step-by-Step Guide

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can build a full-stack web app with Rust and WebAssembly by using a Rust web framework for the server and compiling a Rust frontend to WebAssembly with a framework like Yew or Leptos. The shortest working set is: install Rust and the wasm32-unknown-unknown target, create a workspace with a server crate (Axum) and a client crate (Yew), serve the built WASM bundle from the server, and share types between them.

What You'll Need

  • A computer running Windows, macOS, or Linux with a terminal.
  • Rust installed via rustup (the official installer from rust-lang.org).
  • Node.js and npm only if you want extra tooling like wasm-pack helpers; not strictly required for the workflow below.
  • A code editor such as VS Code with the rust-analyzer extension.
  • Basic familiarity with Cargo, HTML, and HTTP.

Menu labels and tool names can differ slightly by Rust version, operating system, and region, so treat the exact wording as a guide.

Step-by-Step

  1. Install the Rust toolchain. Download rustup from rust-lang.org and run the installer. Verify with rustc --version and cargo --version in a new terminal.
  2. Add the WebAssembly target. Run rustup target add wasm32-unknown-unknown. This is the compilation target your browser can execute.
  3. Install a WASM bundler. Run cargo install trunk. Trunk builds your Rust frontend into HTML, JS, and WASM files. Alternatively install wasm-pack if you prefer that toolchain.
  4. Create a Cargo workspace. Make a folder for the project and a root Cargo.toml with a [workspace] section listing members = ["server", "client", "shared"]. This keeps server, browser code, and common types in one repo.
  5. Create the shared crate. Run cargo new shared --lib. Put request and response structs here, deriving serde::Serialize and serde::Deserialize. Both server and client depend on this crate, so the API contract stays in sync.
  6. Create the server crate. Run cargo new server. Add axum, tokio, serde, and tower-http with the fs feature to its Cargo.toml. In main.rs, build an Axum router with a JSON API route and a fallback that serves static files from the frontend build directory.
  7. Create the client crate. Run cargo new client. Add yew (or leptos) and wasm-bindgen to its Cargo.toml, and set crate-type = ["cdylib", "rlib"] under [lib]. Write a component that fetches from your API using gloo-net and renders the result.
  8. Add a Trunk config. In the client folder create Trunk.toml and an index.html that loads your Rust entry point. Run trunk build to produce a dist folder containing the WASM bundle.
  9. Point the server at the build output. Configure the static file service to serve client/dist. Start the server with cargo run -p server and open http://localhost:3000 in a browser.
  10. Iterate with two terminals. Keep the server running in one terminal and run trunk serve --proxy-backend=http://localhost:3000/api in another for hot reload during development. For production, build the client once and serve the static files from the server.
  11. Add a database when needed. Use sqlx with SQLite or Postgres from the server crate only. Never put database credentials in the client crate, because everything compiled to WASM is downloadable by visitors.

Common Mistakes

  • Forgetting rustup target add wasm32-unknown-unknown, which produces a confusing build error about the target not being installed.
  • Putting secrets, API keys, or database logic in the client crate. Anything in WASM is public.
  • Duplicating request and response types in server and client instead of using a shared crate, which causes silent JSON mismatches.
  • Using server-only crates (filesystem, threads, native TLS) in the client; they do not compile to WASM.
  • Serving the WASM file with the wrong MIME type. Static file servers usually handle this, but hand-rolled handlers often do not.
  • Trying to run cargo run on the client crate directly; the browser cannot run a native binary.

Tips

  • Keep the shared crate dependency-free apart from serde so it compiles for both native and WASM targets.
  • Use #[cfg(target_arch = "wasm32")] to gate browser-only code.
  • Build in release mode (trunk build --release) before measuring bundle size; debug WASM files are much larger.
  • Add a health-check route on the server so you can confirm it is running before debugging the frontend.
  • Log errors on the server and return plain JSON error bodies the client can display.

Troubleshooting

If the page loads but the app is blank, open the browser developer console and check for a WASM loading error or a panic message. A 404 on the .wasm file usually means the static file path is wrong. If the build fails with a target error, re-run the rustup target add command. If API calls fail with a CORS error during development, use Trunk's proxy option instead of calling the server directly from a different port. If the server starts but returns 404 for every route, check the order of your router layers and confirm the fallback is registered. If compilation is very slow, check that you are not pulling heavy native-only dependencies into the client crate.

Frequently Asked Questions

Do I need Node.js to build a Rust WASM frontend?

No. Trunk handles the build and can run without Node.js. Node is only useful if you add JavaScript tooling around the project.

Can I use the same Rust code on server and client?

Yes, for pure logic and data types in a shared crate. Code that touches the filesystem, threads, or native networking must stay on the server.

Which frontend framework should I pick, Yew or Leptos?

Both work. Yew is component-based and closer to React in style; Leptos uses fine-grained reactivity. Pick one and stay with it for the project.

Is WebAssembly faster than JavaScript?

It can be for compute-heavy work, but for typical UI rendering the difference is small. The main benefit here is writing the whole stack in Rust.

Can I deploy this to a normal web host?

Yes. Build the client, then run the Rust server as a binary on any host that supports long-running processes, or serve the static files from a CDN and host the API separately.

Why is my WASM bundle so large?

Debug builds include extra data. Use --release, enable link-time optimization in the release profile, and avoid pulling in large crates you do not need.