Why Rust and AI Are Not Science Fiction, But a Working Tool
\n\nWhen 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\nIn 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\nIf 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\nWhat We'll Need: Tools and Preparation
\n\nBefore writing code, let's figure out what's required to integrate ChatGPT into a Rust application.
\n\nRequired 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),serdeandserde_json(for working with JSON),tokio(async runtime) \n
Creating the Project
\ncargo new rust_chatgpt\ncd rust_chatgpt\n\nLet's add dependencies to Cargo.toml:
[dependencies]\nreqwest = { version = "0.12", features = ["json"] }\ntokio = { version = "1", features = ["full"] }\nserde = { version = "1.0", features = ["derive"] }\nserde_json = "1.0"\n\nNow you can run cargo build — dependencies will be pulled in automatically.
Writing the First Request to ChatGPT in Rust
\n\nThe 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.
Data Structures for the API
\nThe OpenAI API expects JSON with a specific schema. Let's describe it using serde:
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\nNote: Message is used for both sending and receiving — so we added both Serialize and Deserialize attributes.
Async Request
\nNow let's write a function that sends a message and returns a response from ChatGPT:
\n\nuse 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