Skip to main content

Set up Rust for CDB EdgeCompute

The Modern Rust SDK is the recommended approach for new CDB EdgeCompute HTTP applications. It is based on WASI-HTTP, the standard WebAssembly interface for HTTP applications, and uses the wstd crate, which provides Rust bindings for WASI-HTTP.

Rust and Cargo are required. On Windows, also install Visual Studio Build Tools with the Desktop development with C++ workload.

Existing apps built with the edgecompute crate and wasm32-wasip1 use a different setup — covered in the Legacy Rust guide.

Add the WebAssembly target

CDB EdgeCompute runs applications compiled to wasm32-wasip2. Add the target once — it applies to all future builds:

rustup target add wasm32-wasip2

Configure a project

CDB EdgeCompute applications compile to WebAssembly libraries rather than standalone executables, so start with a Rust library project. Two changes from the defaults are needed: the output type must be cdylib (a format the WASI runtime can load), and wstd must be listed as a dependency.

  1. Create the library crate:

    cargo new --lib my-app
    cd my-app
    
  2. Replace the contents of Cargo.toml:

    [package]
    name = "my_app"
    version = "0.1.0"
    edition = "2021"
    
    [lib]
    crate-type = ["cdylib"]
    
    [dependencies]
    wstd = "0.6"
    anyhow = "1"
    

    Without crate-type = ["cdylib"], the build succeeds but the output can't run as a CDB EdgeCompute application.

Verify the toolchain

A minimal handler is enough to confirm the toolchain produces a valid WebAssembly component. Replace src/lib.rs:

use wstd::http::body::Body;
use wstd::http::{Request, Response};

#[wstd::http_server]
async fn main(_request: Request<Body>) -> anyhow::Result<Response<Body>> {
    Ok(Response::builder()
        .status(200)
        .body(Body::from("OK"))?)
}

Build it:

cargo build --release --target wasm32-wasip2

The first build downloads dependencies and takes one to two minutes. When it completes without errors, the toolchain is ready — the compiled binary is at ./target/wasm32-wasip2/release/my_app.wasm.