The docs have almost zero explanation of how to use this... when I copy the code block they provide into my vs code editor I get a red line under "EnumString".
#[derive(EnumString)]
pub enum Color {
Red,
// The Default value will be inserted into range if we match "Green".
Green { range:usize },
// We can match on multiple different patterns.
#[strum(serialize="blue",serialize="b")]
Blue(usize),
// Notice that we can disable certain variants from being found
#[strum(disabled="true")]
Yellow,
}
When I click try to "generate impl" it just gives me an empty block:
impl Color {
}
In the docs there is some commented out code, a lot more, and it says this should be generated...
/*
//The generated code will look like:
impl std::str::FromStr for Color {
type Err = ::strum::ParseError;
fn from_str(s: &str) -> ::std::result::Result<Color, Self::Err> {
match s {
"Red" => ::std::result::Result::Ok(Color::Red),
"Green" => ::std::result::Result::Ok(Color::Green { range:Default::default() }),
"blue" | "b" => ::std::result::Result::Ok(Color::Blue(Default::default())),
_ => ::std::result::Result::Err(::strum::ParseError::VariantNotFound),
}
}
}
*/
huh?
In the docs it says this should be auto-derived, so which is it? Why can't I just add #[derive(EnumString)] and have all the stuff I don't care about happen under the hood? Why does my generated code not contain the proper code in the impl? Why is there no explanation of how the heck this strange crate it supposed to be used?
What's the error you're getting? You need to import the EnumString derive macro, either from strum_macros or by enabling the derive feature on strum and importing it from there
You said you had a red line under EnumString though, which is usually an error produced during proc macro expansion. Do you see anything about that when you run cargo check?
@semicoleoncargo check seems to just give me the exact same error:
error[E0599]: no variant or associated item named `from_str` found for enum `Color` in the current scope
--> src/bin/pizza_ordering/prompt_coordinator.rs:25:31
|
25 | Some(s) => Color::from_str(s).unwrap(),
| ^^^^^^^^ variant or associated item not found in `Color`
|
::: src/bin/pizza_ordering/types.rs:10:1
|
10 | pub enum Color {
| ------------------ variant or associated item `from_str` not found for this enum
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
3 | use std::str::FromStr;
|
For more information about this error, try `rustc --explain E0599`.
note: I already have use std::str::FromStr; in my file...
note: I already have use std::str::FromStr; in my file...
in which file exactly?
i don't want to be captain obvious, but, is it the src/bin/pizza_ordering/prompt_coordinator.rs that error message complains about, and gives the suggested solution?
(adding the use of FromStr inside of prompot_coordinator.rs, to allow use of the trait's function, that's implemented in your types.rs with derive macro...)
you might have been looking on docs page of some previous/old version?
i see no references to extern crate on docs.rs neither crates.rs when i search for strum and open latest v0.24.1.
I think that was it. For some reason the 0.15.0 crates.io page was the #1 hit on google. pretty confusing though, especially because there are zero examples on github or docs.rs
I find this happens pretty often and is a pitfall you have to learn to watch out for. There is a little warning symbol on the top of docs.rs that I tend to watch for to let me know I'm looking at out of date documentation, but it looks like crates.io doesn't have something similar.