Start learning free

LESSON 5 OF 414 · WELCOME TO RUST

Syntax Errors in Rust: Reading Compiler Messages

Lesson Read the full explanation

TL ; DR:

  • Syntax errors are mistakes in the use of the Rust language, analogous to spelling or grammar mistakes in a language like English. For example:


  • Omitting quotes when working with strings:

    println!("Welcome!);
    

  • Omitting ! when using the println macro:

    println("Let's code!");
    

  • When Rust encounters a syntax error, it refuses to compile your code and reports an error instead, so the program never runs.




Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing Rust code:


The syntax error:

Syntax errors are mistakes in the use of the Rust language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.

Syntax error means there is something wrong with the way your program is written. Some examples:

1. Misuse of commas when declaring a string:

let greeting = 'Hey there!";

When we compile this code we'll get an error[E0765]: unterminated double quote string.

2. Leaving out an important character:

println!("Hey";

When we compile this code we'll get an error: mismatched closing delimiter that marks the opening ( as an unclosed delimiter.


The reference error:

A reference error occurs when the Rust compiler sees a word it does not recognize. A common example:

Using a non-existent variable / Misspelling a variable's name:

let animal = "parrot";
println!("{}", aniimal);

When we compile this code we'll get an error[E0425]: cannot find value `aniimal` in this scope.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    RUST · WELCOME TO RUST

    Syntax Errors in Rust, explained

    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

    1. 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.
    2. Read the headline. It begins with error and often a code like E0425, followed by a plain-English description.
    3. 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.
    4. Check the help lines. They often show the exact fix.
    5. 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:

    1. On the first line, the string is closed, but the macro call is not: the ) is missing before the semicolon. Add it.
    2. 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 help text.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free