Rust errors / Names, paths and imports
Rust compiler error · checked with rustc 1.99.0
Rust error E0005: refutable pattern in local binding
What it meansA let pattern might not match (like let Some(x) = opt;). Use if let, match or let ... else.
What rustc prints
error[E0005]: refutable pattern in local binding --> main.rs:4:5 | 4 | let Some(y) = x; | ^^^^^^^ pattern `None` not covered | = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant = note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html = note: the matched value is of type `Option<i32>` help: you might want to use `let...else` to handle the variant that isn't matched | 4 | let Some(y) = x else { todo!() }; | ++++++++++++++++
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
Why it happens
- Destructuring an Option or enum with plain
let.
How to fix it
1. let else
Handle the miss and bail out.
let Some(user) = find(id) else {
return Err("no such user".into());
};Other wordings of this error
refutable pattern in local binding, pattern `None` not covered, let else
The compiler team's explanation
Patterns used to bind names must be irrefutable, that is, they must guarantee that a name will be extracted in all cases.
Erroneous code example:
let x = Some(1);
let Some(y) = x;
// error: refutable pattern in local binding: `None` not covered
If you encounter this error you probably need to use a match or if let to deal with the possibility of failure. Example:
let x = Some(1);
match x {
Some(y) => {
// do something
},
None => {}
}
// or:
if let Some(y) = x {
// do something
}From rustc --explain E0005 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
Up next
Often hit nextE0425: cannot find value in this scopeA related error from the same family.Read it next →Did this answer your question?