Mistakes are normal, and the compiler explains them
Everyone writes code that doesn't work at first. Rust helps by checking your program before it runs. The compiler reads the whole program, and if it finds a mistake, it prints an error message and stops. No output is produced, because the program never starts. This is different from languages that run line by line and fail only when they reach the broken line.
Rust's messages are detailed. They show the offending line, mark the exact spot, and often suggest a fix. Learning to read them is one of the most useful early skills.
Syntax errors
A syntax error means the code is not written according to the rules of the language, like a sentence with a missing word. The compiler can't even work out what you meant. The most common causes for a beginner are three kinds of missing pieces.
A missing or mismatched quote
Every string needs an opening and a closing double quote:
println!("I love coding!);
Without the closing quote, the string runs on to the end of the file, and the compiler reports "unterminated double quote string". The same happens when you start with a single quote and end with a double one, as in 'Hey there!", since single quotes are only for single characters such as 'a'.
A missing parenthesis
Every opening ( needs a closing ):
println!("Hello!";
Here the call is never closed, so the compiler reports an "unclosed delimiter" and points at the opening parenthesis. Because the next closing bracket that appears doesn't match it, you may also see "mismatched closing delimiter". Fix the line, and the message goes away.
A missing semicolon
Rust statements end with ;. If you forget it between two statements:
let a = 1
println!("{}", a);
the compiler reports "expected ;, found println" and suggests adding the semicolon.
Other mistakes the compiler catches
Not every compile error is a syntax error. These are reported before the program runs too, and they matter just as much.
Leaving out the !
println is a macro, so it is called with a !. Without it, the compiler says it expected a function but found a macro, and suggests the fix:
println("Let's code!");
A misspelled name
Using a name that doesn't exist is an error of its own:
let animal = "parrot";
println!("{}", aniimal);
The compiler reports "cannot find value aniimal in this scope" and also suggests that a local variable with a similar name exists, animal.
Mismatched types
Writing let age: i32 = "31"; is valid syntax, but the value is text and the variable is a number. The compiler says "mismatched types: expected i32, found &str".
How to read an error message
- Start with the first error. Later errors are often side effects of the first one, such as a missing quote swallowing the lines after it. Fix the first, recompile, and see what remains.
- Read the headline. It begins with
errorand often a code likeE0425, followed by a plain-English description. - Look at the marked line. The arrow and the carets show where the compiler got confused. The real mistake can be a line or two earlier than the marked spot, as with an unclosed parenthesis or quote.
- Check the
helplines. They often show the exact fix. - Warnings are not errors. A warning, such as an unused variable, doesn't stop the program from building.
The lesson's exercise
The exercise gives you two println! lines. Each has a small syntax error, so the compiler rejects the program. The goal is to make it print Hello! and I love coding!:
println!("Hello!";
println!("I love coding!);
Check each line for a missing partner:
- On the first line, the string is closed, but the macro call is not: the
)is missing before the semicolon. Add it. - On the third line, the string is missing its closing quote. Add the
"before the).
println!("Hello!");
println!("I love coding!");
Now the program prints:
Hello!
I love coding!
Key takeaways
- Rust checks the whole program before running it, so a compile error means nothing runs at all.
- A syntax error is a break in the language's rules: a missing quote, parenthesis or semicolon.
- A name that doesn't exist, a missing
!and mismatched types are different errors, and the compiler also catches them early. - Read the first error first, look at the lines it marks, and use the
helptext.
