Using the Rust Runtime with Vercel functions
Use Rust to build high-performance, memory-safe serverless functions. The Rust runtime runs on Fluid compute for optimal performance and lower latency.
- Configure your project - Add a
Cargo.tomlfile with required dependencies - Create your function - Write handlers in the
api/directory - Deploy - Push to GitHub or use the Vercel CLI
Create a Cargo.toml file in your project root:
[package]
name = "rust-hello-world"
version = "0.1.0"
edition = "2024"
[dependencies]
tokio = { version = "1", features = ["full"] } # async runtime
vercel_runtime = { version = "2" } # handles communicating with Vercel's function bridge
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
# Each handler has to be specified as [[bin]]
# Note that you need to provide unique names for each binary
[[bin]]
name = "hello"
path = "api/hello.rs"
# This section configures settings for the release profile, which optimizes the build for performance.
[profile.release]
codegen-units = 1
lto = "fat"
opt-level = 3Create Rust files in your api/ directory. Each file becomes a serverless function:
use serde_json::{Value, json};
use vercel_runtime::{Error, Request, run, service_fn};
#[tokio::main]
async fn main() -> Result<(), Error> {
let service = service_fn(handler);
run(service).await
}
async fn handler(_req: Request) -> Result<Value, Error> {
Ok(json!({
"message": "Hello, world!",
}))
}For more code examples, please refer to our templates:
Push your code to a connected GitHub repository for automatic deployments.
Deploy directly using the Vercel CLI:
vercel deployFor prebuilt deployments, optimize your .vercelignore:
# Ignore everything in the target directory except for release binaries
target/**
!target/release
!target/x86_64-unknown-linux-gnu/release/**
!target/aarch64-unknown-linux-gnu/release/**Feature | Rust Runtime |
|---|---|
| Request metrics |
Was this helpful?