Closed
Description
The snippet below fails to compile with the following messages of each enum
variant:
pub enum State {
Init,
NewProject,
NewTask,
NewEvent,
}
impl State {
pub fn commands<'a>(&self) -> usize {
match self {
Self::Init => 1,
Self::NewProject | Self::NewTask | Self::NewEvent => 2,
_ => 0,
}
}
}
fn main() {}
error[E0599]: no variant named `Init` found for type `State` in the current scope
--> src/main.rs:11:13
|
1 | pub enum State {
| -------------- variant `Init` not found here
...
11 | Self::Init => 1,
| ^^^^^^^^^^ variant not found in `State`
|
= note: did you mean `variant::Init`?
The solution is to replace Self::Init
with explicit type State::Init
. But the issues are:
- Error E0599 doesn't explain the actual problem (Use of
Self
instead of the explicit type) - The message
no variant named "Init" found for type "State" in the current scope
is misleading note: did you mean "variant::Init"?
is also a little misleading. A phrase likenote: replace "Self::Init" with "State::Init"
is much clearer
And finally:
- Why isn't
Self::
allowed insideimpl MyEnum
blocks?