0Pricing
WebAssembly (WASM) for High Performance Apps · Lezione

Configurare Rust per WebAssembly

Configuri l'ambiente di sviluppo Rust per i target WebAssembly e inizializzi un nuovo progetto WASM.

Configurare Rust per WebAssembly è una lezione WebAssembly (WASM) for High Performance Apps gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebAssembly (WASM) for High Performance Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Rust & WebAssembly Setup

Welcome! Rust is a fantastic language for building high-performance WebAssembly (WASM) modules.

Its focus on speed, memory safety, and control makes it ideal for web applications needing native-like performance.

In this lesson, we'll configure your environment and create your first Rust-WASM project.

Verify Rust & Cargo

Before we dive into WASM, ensure you have the Rust toolchain installed. This includes Rustc (the compiler) and Cargo (the package manager).

  • You can check by typing rustc --version and cargo --version in your terminal.
  • If not installed, visit rustup.rs to get rustup, the official Rust installer.

First Rust Program

Let's confirm your Rust setup is working. Create a file named main.rs and add this code. Then run it using cargo run.

fn main() {
  println!("Hello from Rust!");
}

Install WASM Target

WebAssembly isn't a default target for Rust. You need to explicitly add it to your toolchain.

The wasm32-unknown-unknown target is a generic WASM target that doesn't assume any specific host environment (like a browser or Node.js).

Run this command in your terminal:

rustup target add wasm32-unknown-unknown

Meet wasm-pack

Building Rust code for WebAssembly can involve several steps. That's where wasm-pack comes in!

wasm-pack is a powerful command-line tool that streamlines the entire workflow: compiling your Rust code, generating JavaScript bindings, and packaging it for web usage.

Install wasm-pack Tool

Since wasm-pack is a Rust application, you can install it using Cargo, Rust's package manager.

Run this command in your terminal:

cargo install wasm-pack

This might take a moment as it compiles the tool.

Initialize WASM Project

Now let's create a new Rust project specifically for WebAssembly. wasm-pack can scaffold this for you.

Navigate to your desired projects directory and run:

wasm-pack new my-first-wasm-app

This command creates a new directory, my-first-wasm-app, with a basic Rust library structure ready for WASM.

Explore Project Files

Inside your new my-first-wasm-app directory, you'll find:

  • Cargo.toml: Rust's manifest file, defining project dependencies and metadata.
  • src/lib.rs: This is where your Rust code for the WASM module will live. Unlike traditional Rust applications, WASM modules are usually libraries.
  • .gitignore: Standard Git ignore file.

Simple WASM Function

Open src/lib.rs. You'll see some boilerplate. Let's add a simple function that can be called from JavaScript:

The #[wasm_bindgen] attribute tells wasm-bindgen (which wasm-pack uses) to generate JavaScript glue code for this function.

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
  format!("Hello, {}!", name)
}

// You might also find a #[wasm_bindgen]
// macro for a `start` function or other
// initial setup in a new project.

Compile to WASM

With your Rust WASM code ready, it's time to compile it into a WebAssembly module and its JavaScript bindings.

Navigate into your project directory (my-first-wasm-app) and run:

wasm-pack build

This command compiles your Rust code, generates the necessary .wasm and .js files, and places them in a new pkg directory.

WASM Setup Quiz

You've learned about the essential tools for Rust WebAssembly development. Which of the following commands are crucial for setting up a Rust environment for WASM and creating a new project?

Recap: Rust WASM Setup

Great job! You've successfully set up your Rust environment for WebAssembly development.

We covered:

  • Verifying your Rust toolchain.
  • Adding the wasm32-unknown-unknown target.
  • Installing and using wasm-pack to initialize and build your first WASM project.
  • Understanding the basic project structure and a simple #[wasm_bindgen] function.

Next, you'll learn how to load and run your compiled WASM module in a web browser using JavaScript!

Domande Frequenti

La lezione «Configurare Rust per WebAssembly» è gratuita?

Sì — il testo completo di «Configurare Rust per WebAssembly» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebAssembly (WASM) for High Performance Apps, passa a CoddyKit PRO. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.

Cosa imparerò in «Configurare Rust per WebAssembly»?

Configuri l'ambiente di sviluppo Rust per i target WebAssembly e inizializzi un nuovo progetto WASM. Eserciti WebAssembly (WASM) for High Performance Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare WebAssembly (WASM) for High Performance Apps?

Non è richiesta alcuna esperienza precedente. WebAssembly (WASM) for High Performance Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Configurare Rust per WebAssembly»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione WebAssembly (WASM) for High Performance Apps?

Sì. Ogni lezione WebAssembly (WASM) for High Performance Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Configurare Rust per WebAssembly
  2. Scrivere funzioni Rust per WASM
  3. Interop efficiente tra JS e `wasm-bindgen`
  4. Gestire gli errori oltre il confine WASM
← Torna a WebAssembly (WASM) for High Performance Apps