Common errors
Compiler errors such as E0382 use of moved value, E0502, E0308 mismatched types, and panics such as index out of bounds and unwrap on None.
Runs in Zerethon IDE · Rust 1.87
Official Rust documentation ↗
Most mistakes are compile errors. Each has a code; the message points at the line and often suggests the fix. A runtime failure is a panic: thread 'main' panicked at main.rs:7:21: followed by the reason, and the program exits with status 101.
error[E0382]: borrow of moved value— the value was moved to another variable or function. Borrow with&instead, or.clone()it.error[E0502]: cannot borrow 'v' as mutable because it is also borrowed as immutable— finish using the&borrow before changing the value (e.g. do notpushwhile looping over&v).error[E0384]: cannot assign twice to immutable variable— declare it withlet mut.error[E0308]: mismatched types— convert withas,.to_string(),parse(); or remove a;after the value a function should return.error[E0425]: cannot find value 'x' in this scope— a typo, or the variable belongs to another block.index out of bounds: the len is 3 but the index is 5— usev.get(i)when the index may be too large.called Option::unwrap() on a None value/called Result::unwrap() on an Err value— handle the missing or failed case withmatch,if letorunwrap_or.
main.rs
fn main() {
let mut total = 0;
for v in ["10", "x", "7"] {
match v.parse::<i32>() {
Ok(n) => total += n,
Err(_) => println!("skipped {v:?}"),
}
}
println!("total: {total}");
}
Output
skipped "x" total: 17
match on the Result, instead of unwrap().