Rust and AI: How to Use ChatGPT in Rust Development

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Rust and AI Are Not Science Fiction, But a Working Tool

\n\n

When we hear "artificial intelligence" and "Rust" in the same sentence, we usually imagine complex neural networks written in Python. But what if I told you that Rust can be an excellent backend for interacting with ChatGPT? Without slow Python, without dependencies like TensorFlow — just clean, fast, safe code that communicates with the OpenAI API.

\n\n

In this article, we won't be training a neural network from scratch — we'll do something more practical: write a Rust program that sends requests to ChatGPT and receives responses. Then we'll teach it to generate code, documentation, and even fix errors. Sounds like magic? In reality, it's just a couple of crates and the right API key.

\n\n

If you've ever wanted to add "brains" in the form of a language model to your Rust project — you've come to the right place. Let's go!

\n\n

What We'll Need: Tools and Preparation

\n\n

Before writing code, let's figure out what's required to integrate ChatGPT into a Rust application.

\n\n

Required Components

\n
    \n
  • Rust — installed compiler (rustc) and Cargo package manager. If you don't have Rust yet — curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • \n
  • OpenAI API key — register at platform.openai.com, create a key and put it in the environment variable OPENAI_API_KEY
  • \n
  • Rust libraries — we'll use the crates reqwest (for HTTP requests), serde and serde_json (for working with JSON), tokio (async runtime)
  • \n
\n\n

Creating the Project

\n
cargo new rust_chatgpt\ncd rust_chatgpt
\n\n

Let's add dependencies to Cargo.toml:

\n\n
[dependencies]\nreqwest = { version = "0.12", features = ["json"] }\ntokio = { version = "1", features = ["full"] }\nserde = { version = "1.0", features = ["derive"] }\nserde_json = "1.0"
\n\n

Now you can run cargo build — dependencies will be pulled in automatically.

\n\n

Writing the First Request to ChatGPT in Rust

\n\n

The simplest way to "talk" to ChatGPT is to send a POST request to the endpoint https://api.openai.com/v1/chat/completions. Rust with its strong typing is perfect for this task: we'll clearly describe the request and response structure, and the compiler will check us at every step.

\n\n

Data Structures for the API

\n

The OpenAI API expects JSON with a specific schema. Let's describe it using serde:

\n\n
use serde::{Deserialize, Serialize};\n\n#[derive(Serialize)]\nstruct ChatRequest {\n    model: String,\n    messages: Vec<Message>,\n    temperature: f32,\n}\n\n#[derive(Serialize, Deserialize, Clone)]\nstruct Message {\n    role: String,\n    content: String,\n}\n\n#[derive(Deserialize)]\nstruct ChatResponse {\n    choices: Vec<Choice>,\n}\n\n#[derive(Deserialize)]\nstruct Choice {\n    message: Message,\n}
\n\n

Note: Message is used for both sending and receiving — so we added both Serialize and Deserialize attributes.

\n\n

Async Request

\n

Now let's write a function that sends a message and returns a response from ChatGPT:

\n\n
use std::env;\n\nasync fn ask_chatgpt(prompt: &str) -> Result<String, Box<dyn std::error::Error>> {\n    let api_key = env::var("OPENAI_API_KEY")\n        .expect("OPENAI_API_KEY environment variable not set");\n\n    let client = reqwest::Client::new();\n\n    let request_body = ChatRequest {\n        model: "gpt-4o-mini".to_string(), // fast and cheap model\n        messages: vec![\n            Message {\n                role: "system".to_string(),\n                

Blogs

Book Recommendations