Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0170: pattern binding `GET` is named the same as one of the variants of the type `Method`
What it meansA pattern binding is using the same name as one of the variants of a type.
What rustc prints
error[E0170]: pattern binding `GET` is named the same as one of the variants of the type `Method` --> main.rs:8:9 | 8 | GET => true, | ^^^ help: to match on the variant, qualify the path: `Method::GET` | = note: `#[deny(bindings_with_variant_name)]` on by default
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.
The compiler team's explanation
A pattern binding is using the same name as one of the variants of a type.
Erroneous code example:
enum Method {
GET,
POST,
}
fn is_empty(s: Method) -> bool {
match s {
GET => true,
_ => false
}
}
fn main() {}
Enum variants are qualified by default. For example, given this type:
enum Method {
GET,
POST,
}
You would match it using:
enum Method {
GET,
POST,
}
let m = Method::GET;
match m {
Method::GET => {},
Method::POST => {},
}
If you don't qualify the names, the code will bind new variables named "GET" and "POST" instead. This behavior is likely not what you want, so rustc warns when that happens.
Qualified names are good practice, and most code works well with them. But if you prefer them unqualified, you can import the variants into scope:
use Method::*;
enum Method { GET, POST }
If you want others to be able to import variants from your module directly, use pub use:
pub use Method::*;
pub enum Method { GET, POST }From rustc --explain E0170 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0308mismatched types
- E0277the trait bound is not satisfied
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0131The `main` function was defined with generic parameters
- E0158constant pattern cannot depend on generic parameters
- E0178expected a path on the left-hand side of `+`
- E0183manual implementations of `FnOnce` are experimental
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?