Answer#
The assistant is most useful when it sees the failing code and the compiler's message together. The compiler is the judge: whatever the assistant proposes has to build, and the error text often names the fix already.
How to do it#
- Take a file that fails to build, for example this one:
fn shout(s: String) -> String {
s.to_uppercase()
}
fn main() {
let name = String::from("ada");
let loud = shout(name);
println!("{loud} {name}");
}- Make sure the file is open in the editor. The Fix errors quick action sends that file (or the selected lines) together with the problems of the file, as lines such as
line 8: borrow of moved value: name (error). Only the first line of each message is included, so for a long error paste the whole compiler output into the question, from the Output panel or fromcargo buildin the Terminal. - Press Fix errors. It sends the prompt "Fix the problems listed above. Give the corrected code." You can also write your own, for instance: "Fix this without cloning the String."
- Look at the code block in the reply. If it carries a file path, Apply puts it into the editor (see the thread on applying code); otherwise use Insert or Copy.
- Build again. This version compiles and runs:
fn shout(s: &str) -> String {
s.to_uppercase()
}
fn main() {
let name = String::from("ada");
let loud = shout(&name); // lend it instead of giving it away
println!("{loud} {name}");
}ADA adaWhy it works#
The compiler message tells the assistant what is wrong and where; the code tells it what you meant. Without the message the assistant has to guess which of the several possible fixes you want. A request with a constraint ("without cloning", "keep the function signature") gets a better answer than "fix it".
Common mistakes#
- Accepting a fix you cannot explain. If the answer adds
clone(),Rcorunsafe, ask why that is needed; often a reference does the job. The thread on borrowed values lists the usual fixes to compare with. - Sending only the error text, without the code around it.
- Applying a change and not building. Only the compiler can tell you that it works.
In Ruststudio#
Use Ctrl+Shift+I to open the assistant. Run > Check (F7) rebuilds quickly after each change.