Rust Errors
Errors
Everyone makes mistakes when they write code. In earlier chapters, you have seen examples with a red border, which did not work. In this chapter, you will learn which kinds of errors there are in Rust, and how to read an error message.
There are two main kinds of errors:
- Compile errors - the Rust compiler finds a problem before the program runs. The program does not run at all.
- Runtime errors - the program starts, but stops in the middle because something went wrong. In Rust, this is called a panic.
Rust checks a lot of things before the program runs. This means that many mistakes become compile errors, which are easier to find and fix than errors that happen while the program runs.
Compile Errors
In this example, a semicolon is missing at the end of line 2:
Example
fn main() {
let name = "John"
println!("Hello {}", name);
}
Try it Yourself »
The compiler stops with this message:
error: expected `;`, found `println`
--> prog.rs:2:20
|
2 | let name = "John"
| ^ help: add `;` here
3 | println!("Hello {}", name);
| ------- unexpected token
How to Read an Error Message
Rust error messages can look long, but they always follow the same pattern. Let's look at another one. Here, the variable name is spelled wrong (scor instead of score):
Example
fn main() {
let score = 10;
println!("{}", scor);
}
Try it Yourself »
error[E0425]: cannot find value `scor` in this scope
--> prog.rs:3:18
|
3 | println!("{}", scor);
| ^^^^ help: a local variable with a similar name exists: `score`
- The first line tells you what is wrong.
E0425is the error code. You can search for it online to read more about the error. - The second line tells you where the error is:
prog.rs:3:18means the fileprog.rs, line3, column (character)18. In our editor, your code is saved in a file calledprog.rs. - The rest shows the line with the problem. The
^^^^marks the exact place, andhelp:often tells you how to fix it.
Tip: If you get many errors at the same time, start with the first one. Fixing it often removes some of the other errors too.
Common Compile Errors
Here are some errors that you will often see as a beginner. You have seen most of them in earlier chapters:
| Error message | What it means | How to fix it |
|---|---|---|
expected `;` |
A semicolon is missing | Add ; at the end of the statement |
cannot find value |
The variable does not exist here (wrong spelling, or created in another scope) | Check the spelling, and where the variable is created |
mismatched types |
The value has the wrong type | Use a value of the right type |
cannot assign twice to immutable variable |
You changed a variable that is not mutable | Add mut when you create the variable |
borrow of moved value |
You used a value after its ownership was moved | Use a reference (&) or .clone() |
Here is an example of mismatched types:
Example
fn main() {
let age: i32 = "30";
println!("{}", age);
}
Try it Yourself »
error[E0308]: mismatched types --> prog.rs:2:18 | 2 | let age: i32 = "30"; | --- ^^^^ expected `i32`, found `&str` | | | expected due to this
age should be an i32 (a number), but "30" is text, because of the double quotes. To fix it, remove the quotes: let age: i32 = 30;
Runtime Errors (Panics)
Some errors cannot be found by the compiler. The program compiles, but stops when it runs. In Rust, this is called a panic.
A common example is to use a vector index that does not exist:
Example
fn main() {
let numbers = vec![1, 2, 3];
println!("{}", numbers[5]);
}
Try it Yourself »
thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 5', prog.rs:3:18
The vector has 3 items (index 0, 1 and 2), so there is no index 5. The message tells you what happened, and where (line 3, column 18).
Tip: To avoid this panic, you can use the get() method, which returns an Option instead of crashing.
The panic! Macro
You can also stop the program on purpose with the panic! macro. Use it when something happens that the program cannot continue from:
Example
fn main() {
let lives = 0;
if lives == 0 {
panic!("Game over! No lives left.");
}
println!("Keep playing!");
}
Try it Yourself »
thread 'main' panicked at 'Game over! No lives left.', prog.rs:5:5
The text you write inside panic!() is shown in the error message. "Keep playing!" is never printed, because the program stops at the panic.
Note: A panic stops the whole program. In the next chapter, you will learn how to handle errors with Result, so that your program can continue instead.