For example, if I have code like:
enum Foo {
Bar,
Baz,
Bat,
Quux
}
impl Foo {
from(input: &str) -> Foo {
Foo::input
}
}
This will obviously fail because input
is not a method of Foo. I can manually type:
from(input: &str) -> Foo {
match(input) {
"Bar" => Foo::Bar,
// and so on...
}
}
but I'm not getting the automatic convenience.
It looks like Java has a string lookup function on enums for this specific purpose.
Is it possible to get this without writing my own macro or importing one from a crate?
"Bar"
you need"Foo::Bar"
or"my_module::Foo::Bar"
or"bar"
? That helps explain why there's no pre-made way to do this. There's nothing in the standard library, beyond that you would just be asking for a library / tool recommendation.