Rust
1. 설정
Rust 툴체인에 wasm32-wasip2 대상을 추가하십시오.
#![allow(unused)]
fn main() {
rustup target add wasm32-wasip2
}
wasmtime을 설치하십시오. Wasmtime CLI에는 WebAssembly HTTP 컴포넌트 제공을 지원하는 HTTP 서버가 내장되어 있습니다.
curl https://wasmtime.dev/install.sh -sSf | bash
2. Rust WebAssembly 프로젝트 생성하기
cargo new로 새 Rust 프로젝트를 생성하십시오:
cargo new wasm-http-hello-world
cd wasm-http-hello-world
Wasm 컴포넌트용 Rust 비동기 표준 라이브러리인 wstd를 cargo add로 의존성에 추가하십시오:
cargo add wstd
wstd는 Rust WebAssembly 컴포넌트의 사용 편의성을 높이기 위해 WASI 표준 인터페이스 (wasi:http)에 대한 관용적인 Rust 바인딩을 제공합니다. wstd를 사용하므로 WIT 파일을 추가하거나 wit-bindgen에 직접 의존할 필요가 없습니다.
Note
wstd없이도 Rust로 HTTP 컴포넌트를 빌드할 수 있습니다.wstd없이 HTTP 컴포넌트를 빌드하려면 컴포넌트의wasi:http임포트/익스포트를 WIT로 정의하고,wkg로 WIT 의존성을 가져오고,wit-bindgen으로 Rust 바인딩을 생성해야 합니다.두 방식 모두 유효하지만
wstd가 더 뛰어난 개발자 경험을 제공하므로 여기서는 이를 사용합니다.
wstd와wit-bindgen은 상호 배타적이지 않으며 같은 프로젝트에서 공존할 수 있습니다.
3. HTTP 핸들러 작성하기
src/main.rs에 HTTP 핸들러를 구현합니다. 파일은 다음과 같아야 합니다:
use wstd::http::{Body, Request, Response, Result, StatusCode};
// WASI HTTP server components don't use a traditional `main` function.
// They export a function named `handle` which takes a `Request`
// argument, and which may be called multiple times on the same
// instance. To let users write a familiar `fn main` in a file
// named src/main.rs, wstd provides this `wstd::http_server` macro, which
// transforms the user's `fn main` into the appropriate `handle` function.
#[wstd::http_server]
async fn main(req: Request<Body>) -> Result<Response<Body>> {
match req.uri().path() {
"/" => home(req).await,
_ => not_found(req).await,
}
}
async fn home(_req: Request<Body>) -> Result<Response<Body>> {
Ok(Response::new("Hello, world!\n".into()))
}
async fn not_found(_req: Request<Body>) -> Result<Response<Body>> {
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(().into())
.expect("builder succeeds"))
}
4. 컴포넌트 컴파일 및 실행하기
컴포넌트 빌드하기:
cargo build --release --target wasm32-wasip2
컴포넌트의 .wasm 바이너리는 target/wasm32-wasip2/release/wasm-http-hello-world.wasm에서 찾을 수 있습니다.
컴포넌트를 실행하려면 컴포넌트 모델을 지원하는 참조 구현 호스트인 wasmtime을 사용할 수 있습니다.
특히 wasmtime serve 하위 명령을 사용할 수 있으며, 이 명령은 http://localhost:8080에서 HTTP 서버를 실행하고 우리 컴포넌트를 사용하여 웹 요청을 처리합니다. wasmtime은 요청을 처리할 때마다 컴포넌트의 새로운 인스턴스를 생성합니다.
wasmtime serve -Scli -Shttp target/wasm32-wasip2/release/wasm-http-hello-world.wasm
curl -i localhost:8080으로 테스트할 수 있습니다
HTTP/1.1 200 OK
transfer-encoding: chunked
date: Mon, 13 Apr 2026 23:22:20 GMT
Hello, world!
이로써 Rust로 기본적인 WebAssembly HTTP 컴포넌트를 성공적으로 빌드하고 실행했습니다 🎉
5. 더 나아가기
wstd를 사용하는 프로젝트의 더 많은 예제를 살펴보십시오: