Why Is Rust Getting More Adoption in AI? (2026)

Why Rust is gaining ground in AI infrastructure while Python stays on top: tokenizer speed, memory safety, no garbage collector, and what to learn first.

By Danyal Ahmed - Data Scientist · · 10 min read

Rust is getting more adoption in AI because the code that runs on every request, such as tokenizers, servers and storage services, has to be both fast and memory safe, and Rust delivers both without a garbage collector. Python is still where you train models, do research and write most AI code, because it is quick to change and the heavy math runs on the GPU anyway. The common pattern in 2026 is Python for the AI logic and Rust for the hot paths called from Python.

This guide explains the idea with a concrete example, covers the three real reasons (speed, memory safety and no garbage collector), shows what a Rust compiler error looks like, lists where Rust is already used in AI tooling, and answers the practical question: should you learn Rust instead of Python for AI? Short answer: usually not.

Key takeaways

  • Python stays the language of AI work. Training, research and notebooks are still Python.
  • Rust takes over the hot, shared, always-on parts: tokenizers, inference servers, gateways and data tools, usually called from Python.
  • Speed: a compiled language avoids the cost of repeating a small step millions of times in Python.
  • Safety: about 70% of Chromium's serious security bugs are memory safety problems, and Rust is designed to prevent that class of bug at compile time.
  • No garbage collector: Rust frees memory when its owner is done, so there are no collection pauses or collector CPU cost.
  • Do not switch languages blindly. Profile first, because many AI workloads are limited by the GPU or the network, not the language.

A simple example: 1,000 questions in one second

Imagine an AI chat app. In one second, 1,000 people send it a question. A language model cannot read words, only numbers. So before the GPU can run the model, your server has to cut every question into small pieces and turn each piece into a number. These pieces are called tokens. For how tokens affect cost and context, see LLM tokens explained.

That cutting step is a tokenizer. It is a small piece of logic repeated millions of times: look up a chunk, merge, repeat. Python is easy to write but slow at tight repeated loops.

OpenAI's tiktoken library is a fast byte-pair encoding tokenizer for its models. Its README says it is "between 3-6x faster than a comparable open source tokeniser" in OpenAI's benchmark on 1 GB of text [1]. The package also contains an educational submodule for learning how byte-pair encoding works [1], which makes a side-by-side comparison possible.

An informal comparison

In one informal laptop test, a simple Python implementation cut 1,000 questions in about a tenth of a second, while the compiled Rust-backed version did the same in about five thousandths of a second, roughly 20 times faster. That is one person's test, not a standard benchmark, and your numbers will vary with the text, the hardware and the implementation. You call the fast version from Python, but the cutting runs in compiled code.

You can try the comparison yourself. This is a sketch, not something we have run:

pythonimport time
import tiktoken
from tiktoken._educational import SimpleBytePairEncoding

questions = ["How do I reset my password?"] * 1000

fast = tiktoken.get_encoding("cl100k_base")
simple = SimpleBytePairEncoding.from_tiktoken("cl100k_base")

t = time.perf_counter()
for q in questions:
    fast.encode(q)
print("tiktoken:", time.perf_counter() - t)

t = time.perf_counter()
for q in questions:
    simple.encode(q)
print("educational:", time.perf_counter() - t)

The imports and function names come from the tiktoken README [1]. Check the current README if anything has changed.

Why not just use C++?

C++ is just as fast, and much of the world's performance-critical software is written in it. The catch is memory safety.

C and C++ give you full control over memory, which also makes mistakes easy. A classic one is use after free: your program gives a piece of memory back, and later uses it again by mistake. The result can be a crash, or an attacker can exploit it to run their own code.

How common is this? The Chromium project reports that "around 70% of our serious security bugs are memory safety problems," and that half of those are use-after-free bugs, based on 912 high or critical severity bugs since 2015 [2]. Microsoft has publicly reported a similar share of its security vulnerabilities being memory safety issues [3]. Google reports that memory safety issues fell below 20% of new vulnerabilities in Android in 2025, down from 76% in 2019, and credits the shift to Rust for new code [4].

The three reasons Rust is winning in AI infrastructure

1. Speed close to C and C++

Rust compiles to native machine code and has no interpreter running your loop. For hot paths such as tokenizing, parsing and serializing, that removes the per-step overhead Python pays. For how much that overhead matters at the hardware level, see how fast the CPU, cache, RAM and SSD are.

2. Memory safety without a garbage collector

Languages like Java and Go avoid most memory mistakes by finding and cleaning up unused memory for you, which is called garbage collection. That cleanup costs extra CPU work while your program runs.

Rust takes a different route. Every value has one owner, and when the owner is done the memory is freed automatically. There is no garbage collector at all. Before your program ever runs, the compiler's borrow checker verifies that nothing uses memory after it has been freed or moved. If something does, the program does not compile.

3. A compiler that gives precise feedback

Compare the same mistake in two languages.

In C, this typically compiles without complaint and fails (or worse, quietly misbehaves) at run time:

cchar *p = malloc(16);
free(p);
p[0] = 'x';   // use after free

In Rust, the equivalent mistake is rejected before the program exists:

rustfn main() {
    let s = String::from("hello");
    drop(s);
    println!("{}", s); // error[E0382]: borrow of moved value: `s`
}

The compiler points to the line and explains the rule that was broken. It does not catch every bug. Logic errors still compile, and Rust code marked unsafe opts out of some checks. But this whole class of memory mistakes is caught before the code goes live.

Why this matters even more with AI coding agents

Now that agents write a lot of code, a strict compiler becomes a feedback tool. Suppose an agent writes a program in C and makes the use-after-free mistake above. The C compiler may build it, and the bug shows up only at run time, perhaps in production.

In Rust, the borrow checker refuses to build it and prints an error naming the line and the rule. The agent reads the error, fixes the line and tries again. This is the same observe-and-adjust cycle described in how loop engineering works, with the compiler acting as the checker. See also agent harness vs agent for how the pieces fit together.

Where Rust is already used in AI and Python tooling

Many popular tools that Python developers use are written in Rust and exposed to Python:

Tool What it does Language core
tiktoken Fast tokenizer for OpenAI models [1] Compiled core, Python interface
Hugging Face tokenizers Fast tokenizers for research and production [5] Rust with Python bindings
safetensors Safe, fast tensor file format [6] Rust
pydantic-core Validation engine behind Pydantic v2 [7] Rust
Polars Fast DataFrame library [8] Rust
uv and Ruff Python package manager and linter [9] Rust

The repositories describe their implementation language, so check each one for the current details. Commentary on 2026 trends also reports that many fast-growing AI agent projects are written in Rust, though much of that commentary is low-quality, so verify any number before you rely on it.

One widely shared account says OpenAI engineers used Codex to rewrite a storage service behind ChatGPT from Python to Rust, with about 6 times less CPU and 15 times less memory. We could not locate a primary source for it, so treat it as unconfirmed.

Python vs Rust for AI: what each is good at

Job Best fit Why
Training and fine-tuning models Python Libraries, notebooks, fast iteration
Research and experiments Python You change code daily
GPU math Runs on the GPU The language is not the bottleneck
Tokenizers and text preprocessing Rust (called from Python) Hot loop at huge scale
Inference servers and gateways Rust, Go or C++ Latency, concurrency, memory
Data pipelines and file formats Rust (via Polars-style tools) Throughput
CLI tools and agent runtimes Rust Single binary, safe, fast
Glue code, APIs, scripts Python or TypeScript Speed of development

An easy way to remember it: you write your AI code in Python, but the code that has to be fast and safe on every request is more and more often Rust.

Pros and cons of Rust for AI work

Pros Cons
Speed close to C and C++ Steep learning curve, especially the borrow checker
Memory safety enforced by the compiler Slower to write and change than Python
No garbage collector, so no collection pauses Smaller machine learning ecosystem than Python
Easy to expose to Python through bindings Longer compile times
Good concurrency safety unsafe blocks still need careful review
Single static binaries are easy to ship Speed-ups vanish if the bottleneck is the GPU or the network

Should you learn Rust instead of Python for AI?

Usually not. Training a model and doing research still happen in Python, the heavy math runs on the GPU anyway, and Python is far quicker to write when you change your code every day.

A sensible path:

  1. Learn Python first. Get comfortable with data and models. See machine learning explained and ML algorithms: linear, logistic, decision tree and KNN.
  2. Learn how LLMs work, including tokens and context. See how large language models work.
  3. Learn Rust basics when you start building tools, servers or performance-critical components: ownership, borrowing and error handling.
  4. Learn the bridge. Libraries such as PyO3 and the build tool maturin are commonly used to write Rust extensions that Python can import.
  5. Keep the Python interface. Wrap Rust in a clean Python API so the rest of your team can use it.

How to decide if you need Rust at all

  1. Profile before you rewrite. Find out where time and memory actually go.
  2. Check for an existing fast library, such as a Rust-backed tokenizer or DataFrame tool, before writing anything yourself.
  3. Rewrite only the hot path, the small loop that dominates the profile.
  4. Benchmark both versions on real data, and compare end-to-end latency, not just one function.
  5. Weigh the maintenance cost. A faster component that nobody on the team can maintain is a risk.
  6. Consider Go, C++ or a faster algorithm too. Sometimes batching or caching beats a rewrite. See caching strategies and LLM cost optimization.

For wider language trade-offs, see why there are so many programming languages, and for Python startup tuning, read Python lazy imports.

Common myths

  • "Rust is replacing Python in AI." No. Python remains the main language for model work, and Rust complements it in infrastructure.
  • "Rust makes everything faster." Only where the language is the bottleneck. GPU-bound and network-bound work will not speed up much.
  • "Rust programs cannot have bugs." It prevents a class of memory bugs, not logic errors, race conditions in your design or mistakes in unsafe code.
  • "You must rewrite everything." Most teams rewrite one hot path and keep the rest.

Building an AI product that needs to be fast and reliable?

We build AI agents, backends and data pipelines, and we profile before we optimise. Get a scope estimate for your AI project, read about our AI agent development service, or see the backend developer roadmap.

FAQ

Why is Rust getting more adoption in AI?

Because AI infrastructure has hot paths, such as tokenizers, servers and storage, that run on every request and need both speed and memory safety. Rust provides both without a garbage collector, and it can be called from Python.

Is Rust replacing Python in AI?

No. Python remains the main language for training, research and application logic. Rust is increasingly used for the performance-critical components underneath.

Is Rust faster than Python?

For compute-heavy loops, usually yes, often by a large factor, because it compiles to native code. One informal tokenizer comparison showed roughly 20 times, but results vary, and GPU-bound or network-bound work sees little benefit.

Why not use C++ instead of Rust?

C++ is equally fast but allows memory mistakes such as use after free. Chromium reports about 70% of its serious security bugs are memory safety problems. Rust prevents that class of bug at compile time.

What is the borrow checker?

The part of the Rust compiler that verifies, before the program runs, that no data is used after it was freed or moved. If a rule is broken, the program does not compile.

Does Rust have a garbage collector?

No. Each value has one owner, and Rust frees the memory automatically when the owner is done.

Should I learn Rust or Python for AI?

Learn Python first, since training and research happen there. Learn Rust later if you build infrastructure, tools or performance-critical services.

How do Python and Rust work together?

Rust code is compiled into a library that Python can import, typically using bindings built with tools such as PyO3 and maturin, so you call fast Rust code from Python.

References

  1. OpenAI. "tiktoken: a fast BPE tokeniser for use with OpenAI's models." GitHub. github.com/openai/tiktoken
  2. The Chromium Projects. "Memory safety." chromium.org/Home/chromium-security/memory-safety
  3. Microsoft Security Response Center. "Why Rust for safe systems programming." 2019. msrc.microsoft.com/blog/2019/07/why-rust-for-safe-systems-programming
  4. Google Security Blog. "Rust in Android: move fast and fix things." November 2025. blog.google/security/rust-in-android-move-fast-fix-things
  5. Hugging Face. "tokenizers." GitHub. github.com/huggingface/tokenizers
  6. Hugging Face. "safetensors." GitHub. github.com/huggingface/safetensors
  7. Pydantic. "pydantic-core." GitHub. github.com/pydantic/pydantic-core
  8. Polars. GitHub. github.com/pola-rs/polars
  9. Astral. "uv" and "Ruff." GitHub. github.com/astral-sh/uv

Method and limits: figures come from the sources above, read in October 2026. The 20x tokenizer comparison is an informal laptop test and the ChatGPT storage rewrite is an unconfirmed report. Always benchmark your own workload.