---
title: "Write Your First Rust Contract"
description: "Use the GenLayer Rust SDK to write a contract that takes a name and returns a greeting. GenVM runs the contract as a WebAssembly (Wasm) binary."
source: https://docs.genlayer.com/developers/intelligent-contracts/first-rust-contract
last_updated: 2026-12-05
---

# Write Your First Rust Contract

Use the [GenLayer Rust SDK](https://crates.io/crates/genlayer_sdk) to write a contract that takes a name and returns a greeting. GenVM runs the contract as a WebAssembly (Wasm) binary.

> **Warning:**
> The Rust SDK is experimental. This guide builds a local Wasm artifact. Check that your target network and deployment tools accept Rust Wasm artifacts before trying to deploy it.

## Set up the project

With a stable Rust toolchain installed through `rustup`, add the WASI target and create a binary project.

```bash
rustup target add wasm32-wasip1
cargo new --bin --edition 2024 hello-genlayer
cd hello-genlayer
```

Replace `Cargo.toml` with the following manifest.

```toml
[package]
name = "hello-genlayer"
version = "0.1.0"
edition = "2024"

[dependencies]
bytes = "1"
genlayer_sdk = "=0.0.2"
```

## Implement the contract

Replace `src/main.rs` with the following code.

```rust
use bytes::Bytes;
use genlayer_sdk::abi::entry::MessageData;
use genlayer_sdk::abi::entry::contract_def::{ConsensusStageData, Contract, MainCalldata};
use genlayer_sdk::calldata::Value;

#[derive(Default)]
struct HelloContract;

impl Contract for HelloContract {
    fn handle_main(&mut self, message: MessageData, data: Bytes) -> Result<Value, String> {
        if message.is_init {
            return Ok(Value::Null);
        }

        let call = MainCalldata::parse(&data).map_err(|error| error.to_string())?;
        if call.method != "greet" {
            return Err(format!("unknown method: {}", call.method));
        }

        let name = match (call.args.as_slice(), call.kwargs.is_empty()) {
            ([Value::Str(name)], true) => name,
            _ => return Err("greet expects one positional string argument".into()),
        };

        Ok(Value::Str(format!("Hello, {name}!")))
    }

    fn handle_sandbox(&mut self, _message: MessageData, _data: Bytes) -> Result<Vec<u8>, String> {
        Err("this contract does not use a sandbox".into())
    }

    fn handle_consensus_stage(
        &mut self,
        _message: MessageData,
        _data: Bytes,
        _stage: ConsensusStageData,
    ) -> Result<Value, String> {
        Err("this contract does not run a nondeterministic consensus stage".into())
    }
}

genlayer_sdk::contract_main!(HelloContract);
```

The `contract_main!` macro wires the GenVM entry points to the `Contract` implementation. It constructs a fresh `HelloContract` for each invocation, which is why the struct derives `Default`.

`handle_main` handles deployment when `message.is_init` is true. For regular calls, `MainCalldata::parse` decodes the method name and arguments. Calling `greet` with the positional argument `"Ada"` returns `"Hello, Ada!"`. This example uses deterministic execution and stores no state.

## Build the Wasm artifact

```bash
cargo build --release --target wasm32-wasip1
```

Cargo writes the binary to `target/wasm32-wasip1/release/hello-genlayer.wasm`. GenVM forbids floating-point operations during deterministic execution.
