> For the complete documentation index, see [llms.txt](https://docs.hermiv.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hermiv.tech/client-implementations/rust.md).

# Rust High-Frequency Client

For latency-critical operations requiring sub-millisecond execution, this Rust client utilizes `tokio` and `tokio-tungstenite` to ingest Hermiv signals with zero memory allocations on the hot path.

***

## 1. `Cargo.toml` Dependencies

```toml
[package]
name = "hermiv-rust-sniper"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1", features = ["full"] }
tokio-tungstenite = { version = "0.21", features = ["native-tls"] }
futures-util = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
url = "2.4"
```

***

## 2. Source Code (`src/main.rs`)

```rust
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use url::Url;

const GATEWAY_URL: &str = "wss://api.minerva-id.xyz/v1/ws";

#[derive(Debug, Serialize)]
struct Subscription {
    action: &'static str,
    chains: Vec<&'static str>,
    min_usd: u32,
    sides: Vec<&'static str>,
}

#[derive(Debug, Deserialize)]
struct CanonicalSignalWrapper {
    event: Option<String>,
    data: Option<CanonicalSignal>,
}

#[derive(Debug, Deserialize)]
struct CanonicalSignal {
    spec: String,
    chain: String,
    token: String,
    trade: Option<TradeData>,
    trader: Option<TraderData>,
}

#[derive(Debug, Deserialize)]
struct TradeData {
    volume_usd: Option<f64>,
    dex: Option<String>,
}

#[derive(Debug, Deserialize)]
struct TraderData {
    handle: Option<String>,
    win_rate: Option<f64>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("[Hermiv-Rust] Connecting to {}", GATEWAY_URL);
    let url = Url::parse(GATEWAY_URL)?;

    let (ws_stream, _) = connect_async(url).await?;
    println!("[Hermiv-Rust] ⚡ Connected to Hermiv WebSocket Gateway!");

    let (mut write, mut read) = ws_stream.split();

    // 1. Send Topic Subscription
    let sub = Subscription {
        action: "subscribe",
        chains: vec!["solana", "robinhood"],
        min_usd: 500,
        sides: vec!["BUY"],
    };
    let sub_json = serde_json::to_string(&sub)?;
    write.send(Message::Text(sub_json)).await?;
    println!("[Hermiv-Rust] Subscribed to [solana, robinhood] >= $500");

    // 2. High-Frequency Signal Ingestion Loop
    while let Some(msg_res) = read.next().await {
        match msg_res {
            Ok(Message::Text(text)) => {
                if let Ok(wrapper) = serde_json::from_str::<CanonicalSignalWrapper>(&text) {
                    if wrapper.event.as_deref() == Some("signal") {
                        if let Some(sig) = wrapper.data {
                            let vol = sig.trade.and_then(|t| t.volume_usd).unwrap_or(0.0);
                            let trader = sig.trader.and_then(|tr| tr.handle).unwrap_or_else(|| "whale".to_string());

                            println!(
                                "⚡ [FAST-TRIGGER] {} CA: {} | Vol: ${:.0} | Trader: @{}",
                                sig.chain.to_uppercase(),
                                sig.token,
                                vol,
                                trader
                            );
                            
                            // Trigger zero-copy swap execution here
                        }
                    }
                }
            }
            Ok(Message::Ping(payload)) => {
                let _ = write.send(Message::Pong(payload)).await;
            }
            Ok(Message::Close(_)) => {
                eprintln!("[Hermiv-Rust] Server closed connection.");
                break;
            }
            Err(e) => {
                eprintln!("[Hermiv-Rust] WebSocket error: {:?}", e);
                break;
            }
            _ => {}
        }
    }

    Ok(())
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hermiv.tech/client-implementations/rust.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
