# Rust integration

<Image
  src={rustIcon}
  alt="Rust logo"
  width={100}
  height={100}
  class:list={'float-inline-left icon'}
  data-zoom-off
/>

<Badge text="⭐ Community Toolkit" variant="tip" size="large" />

The Aspire Rust hosting integration enables you to run Rust applications alongside other resources in your AppHost project, providing service discovery, health checks, and observability for your Rust services.

## Prerequisites

This integration requires `cargo` to be installed on your system and available in your `PATH`. For installation instructions, see [Install Rust](https://www.rust-lang.org/tools/install).

:::note
TypeScript AppHost support for this integration isn't yet available.
:::

## Hosting integration

<InstallPackage packageName="CommunityToolkit.Aspire.Hosting.Rust" />

### Add Rust app

In your AppHost, call `AddRustApp` to register a Rust application as a resource. Provide the resource name and the path to the directory containing your Rust project (where `Cargo.toml` lives):

```csharp title="AppHost.cs"
var builder = DistributedApplication.CreateBuilder(args);

var rustApp = builder.AddRustApp("rust-api", workingDirectory: "../rust-app")
    .WithHttpEndpoint(port: 8080, env: "PORT");

builder.AddProject<Projects.ExampleProject>("apiservice")
    .WithReference(rustApp);

builder.Build().Run();
```

The Aspire app host runs `cargo run` in the specified `workingDirectory` to start your Rust application. The required parameters are:

- **name** — the resource name shown in the Aspire dashboard.
- **workingDirectory** — path to the directory containing the Rust application.

### Configure endpoints

Rust applications typically read a `PORT` environment variable to determine which port to listen on. Use `WithHttpEndpoint` to expose an HTTP endpoint and inject the port value as an environment variable:

```csharp title="AppHost.cs"
var builder = DistributedApplication.CreateBuilder(args);

var rustApp = builder.AddRustApp("rust-api", "../rust-app")
    .WithHttpEndpoint(port: 8080, env: "PORT");

builder.Build().Run();
```

Your Rust application can read the injected environment variable as follows:

```rust title="main.rs"
use std::env;
use actix_web::{web, App, HttpServer, Responder};

async fn greet() -> impl Responder {
    "Hello from Rust!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string());
    let addr = format!("0.0.0.0:{}", port);

    println!("Server listening on {}", addr);

    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(greet))
    })
    .bind(addr)?
    .run()
    .await
}
```

### Custom arguments

To pass custom arguments to `cargo run`, chain the `WithArgs` method:

```csharp title="AppHost.cs"
var builder = DistributedApplication.CreateBuilder(args);

var rustApp = builder.AddRustApp("rust-api", "../rust-app")
    .WithArgs("--release")
    .WithHttpEndpoint(port: 8080, env: "PORT");

builder.Build().Run();
```

## See also

- [Rust documentation](https://www.rust-lang.org/learn)
- [Cargo documentation](https://doc.rust-lang.org/cargo/)
- [Aspire Community Toolkit](https://github.com/CommunityToolkit/Aspire)
- [📦 CommunityToolkit.Aspire.Hosting.Rust NuGet package](https://www.nuget.org/packages/CommunityToolkit.Aspire.Hosting.Rust)
- [Aspire integrations overview](/integrations/overview/)