Writing Rust code that's fast by asking agents to make the code faster
In January 2025, the author hypothesized that LLMs could write better code if repeatedly instructed to "write better code". Claude Sonnet 3.5 was able to iteratively improve on Python code, but optimization was not a primary function for agentic LLMs at the time. The author then proposed a hypothetical future where LLMs could write superfast Python code by writing Rust code and using PyO3 for bridging the languages.
After testing and experimenting with Opus 4.5, the author confirms that modern agentic LLMs can indeed write Rust code that is significantly faster than current state-of-the-art methods when given appropriate guardrails and constraints. The author uses Rust as the target language for its Python integration and speed, and a constraint is followed to avoid unsafe code whenever possible.
The author first tested UMAP, a machine learning algorithm for dimensionality reduction, and reimplemented it in Rust. They use criterion to benchmark the algorithm with different input data sizes and run the benchmark suite, manually rerunning after each change to verify no speed regressions. The author then switched to letting the agent be autonomous, iterating until they achieve a speed increase, and refining the prompt to have a clear target goal that can be pass/fail.
Written by urgent.news from Hacker News's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.
- Writing Rust code that's faster than state-of-the-art libraries by asking agents to make the code faster minimaxir.com
- Why do common Rust packages depend on C code? (2023) langdev.stackexchange.com