You paste a borrow checker error into a chat window, hit enter, and get back a working function wrapped in Rc<RefCell<…>> or, worse, a block of unsafe. It compiles. Your test passes. Three weeks later you cannot tell why the value is behind a smart pointer, and the whole point of learning Rust — understanding who owns what — quietly evaporated.
This is the failure mode almost nobody writes about: the LLM does not teach you Rust, it removes the error from your screen. The error was the lesson. This piece is a set of prompts built around a single rule — the model must explain the borrow checker and Rust ownership before it is allowed to touch a line of your code, and if it reaches for unsafe, it has to justify why. Each prompt comes with the compiler error that motivates it, what a good answer looks like, and the test that tells you the model actually understood the problem instead of papering over it.
Why "fix this error" is the worst possible prompt in Rust
Take the most ordinary thing in the world: you have a Vec<String> and you want to keep a reference to its first element while also pushing to it.
fn main() {
let mut names = vec![String::from("ada")];
let first = &names[0];
names.push(String::from("grace"));
println!("{first}");
}
The compiler says:
error[E0502]: cannot borrow `names` as mutable because it is also borrowed as immutable
--> src/main.rs:4:5
|
3 | let first = &names[0];
| ----- immutable borrow occurs here
4 | names.push(String::from("grace"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
5 | println!("{first}");
| ----- immutable borrow later used here
Ask "fix this" and a model will happily return let first = names[0].clone();. It compiles. It is also a completely different program in terms of cost — you now allocate a whole string copy just to dodge a lifetime error, and you have learned nothing about why push invalidates first (it can reallocate the backing buffer, so a pointer into it would dangle).
The real answer here is not a clone and not an Rc. It is: you cannot hold a borrow across a mutation, so decide which of the two you actually need first. Either read first before the push, or index after the push. That is a design decision, and a good prompt forces the model to say so out loud.
Prompt 1: make it walk the borrows, not the error text
The compiler already told you where. You need why, in terms of owners and borrows, not in terms of line numbers.
Prompt:
You are a Rust mentor. I will paste a borrow checker error and the surrounding code. Do NOT write fixed code yet. Instead:
1. List every binding in the function and say, for each, who owns it.
2. Mark every place a borrow starts and where it is last used (the NLL region).
3. Name which borrow conflicts with which, and why the compiler considers them overlapping.
4. Only after that, propose two possible fixes and state the cost of each (extra allocation, runtime check, restructuring).
Here is the code and error: <paste>
What a good answer looks like for the names example: it names names as the owner of the Vec<String>, first as a shared borrow whose region extends to the println!, and the push as a unique borrow that overlaps that region. Then it offers read before push (free) versus clone (one allocation) versus index after push (free, but re-lookup).
The test that it understood: it should mention that push may reallocate, and therefore a reference into the vector cannot survive it. If the answer only says "you can't have both borrows," it recited the error message back at you.
Prompt 2: lifetimes explained on your own struct, not on &str
Every tutorial explains fn longest<'a>(x: &'a str, y: &'a str) -> &'a str. None of them explain the struct you actually wrote, which holds a reference to a config field and refuses to compile.
struct Parser<'a> {
input: &'a str,
}
impl<'a> Parser<'a> {
fn rest(&self) -> &str {
&self.input[1..]
}
}
Prompt:
Explain the lifetimes in this struct as a contract in plain English, no syntax first. Answer concretely:
- What does 'a promise about the Parser value and the input string?
- What breaks if I try to return a Parser that outlives the string it borrows?
- Why does `fn rest(&self) -> &str` compile without naming a lifetime, and what is the elided lifetime actually tied to?
Then, and only then, show the same thing with explicit lifetimes so I can map the English back to the syntax.
The test: the model should say that rest returns a reference tied to &self, not to 'a directly — which is the subtle part everyone gets wrong. If it says "the returned &str lives as long as 'a," it is wrong and you should push back.
Prompt 3: translate the Go or Python idiom, but flag the unsafe trap
This is where LLMs do the most damage. You show a Python pattern — a class holding a reference to a shared list that other code mutates — and the model "translates" it into Rust by reaching for Rc<RefCell<Vec<T>>> everywhere, or unsafe to get a raw pointer around the borrow checker. More on the topic: “Rust Prompts That Make LLMs Write Safe Code: From Borrow Checker to async/await Without the Pain”.
Prompt:
I am porting this Python code to Rust. The Python version shares a mutable list between two objects. Do not transliterate it. Instead:
1. Describe the ownership graph the Python code relies on implicitly.
2. Propose the idiomatic Rust shape: single owner + borrows, or `Rc<RefCell<…>>`, or message passing via channels, or an index/handle. Justify the choice by how many owners there really are and whether mutation happens concurrently.
3. Explicitly forbid `unsafe` in the first proposal. If you believe `unsafe` is genuinely required, stop and explain why the safe options do not fit, with the specific reason.
Python code: <paste>
A good answer for the shared-list case usually lands on one owner plus &mut borrows passed down, with Rc<RefCell<…>> only if two objects genuinely outlive each other and both mutate. The model should say out loud that RefCell moves the borrow check from compile time to runtime and can panic — that is the cost you are paying.
Prompt 4: the ownership audit before you accept any fix
Even a correct-looking fix can quietly change semantics. Before you paste it into your editor, make the model audit its own output.
Prompt:
Here is the fixed code you proposed. Audit it against the original before I use it:
- Did the fix add an allocation, a clone, or a smart pointer? Where exactly and what does it cost at runtime?
- Did the fix change when a value is dropped compared to the original?
- Did it change the order of side effects (prints, IO, mutations)?
- Does it still compile under `#![forbid(unsafe_code)]`?
Answer in a short table: change
| cost | risk.
That last line matters. #![forbid(unsafe_code)] is a real crate-level attribute — put it at the top of lib.rs and the compiler refuses to build if any unsafe sneaks in, including from your own future edits. Making the model answer under that constraint is the cheapest way to keep it honest.
Prompt 5: when it should just tell you to read the docs
The uncomfortable truth: some Rust questions are faster to answer from the official book than from a chat. If you are asking "what is ownership" for the third time, the model will give you a slightly different metaphor each time and you will still not have the mental model. More on the topic: “Rust — Systems Programming: What's Behind the Course and How It Works in Practice”.
Prompt:
Before answering, judge: is this question better served by a short explanation from you, or by me reading a specific chapter of the Rust Book or the std docs? If the latter, name the exact chapter or API page and one sentence on what to look for. Only answer directly if the question is about my specific code.
A good model will tell you that "how do lifetimes work" is a book chapter, while "why does this function fail" is a chat question. That distinction — general concept versus your code — is the honest boundary of where an LLM accelerates learning Rust and where it just adds noise. More on the topic: “Rust for Web (Actix/WASM): Where Rust Is Really Needed and Where You'll Overpay for Speed”.
- Returns a clone or Rc<RefCell<…>>
- Compiles, but semantics change silently
- unsafe appears without justification
- You learn the workaround, not the model
- Names owners and borrow regions
- Offers options with runtime cost
- unsafe forbidden by default
- You learn why the borrow conflicts
Where the LLM genuinely saves you time, and where it wastes it
After running these prompts on real projects, the pattern is clear enough to state plainly.
| Situation | LLM helps? | Why |
|---|---|---|
| Decoding a specific borrow checker error in your code | Yes, strongly | The compiler gives exact locations; the model turns them into an ownership story |
Choosing between Rc, Arc, channels, or restructuring |
Yes | It can lay out the trade-offs and the runtime cost of each |
| Learning lifetimes from scratch | Weakly | You need one coherent source, not five metaphors; read the book |
| Porting a Python/Go idiom | Yes, if you forbid blind transliteration | The value is in rejecting the 1:1 mapping |
| "Make it compile" with no explanation | No — actively harmful | You get unsafe or clones and lose the lesson |
If you want the broader kit — smart pointer choices, async/await ownership, and the trait-object questions that follow the borrow checker — there is a longer collection already on the blog. More on the topic: "Twelve Ways an LLM Earns Its Keep When the Rust Borrow Checker Says No".
The same principle — force the model to prove it understood, not to hide the problem — shows up in other languages too. If you work in Go, the equivalent trap is asking for a fix that adds a mutex everywhere instead of explaining the data race. More on the topic: "Channels or Mutexes? How to Prompt an LLM to Pick the Right Go Concurrency Tool — and Prove It Won't Deadlock".
The borrow checker is not a gatekeeper to get past. It is the compiler teaching you the ownership model one error at a time — and the fastest way to learn it is to make the LLM explain each error instead of deleting it. Write the prompt that demands the explanation, keep #![forbid(unsafe_code)] at the top of your crate, and let the model be your tutor rather than your escape hatch. The next error you hit, do not ask "fix this" — ask "walk the borrows."