How Do I Use The "Strum" Crate?

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".

pub enum Color {

    // The Default value will be inserted into range if we match "Green".
    Green { range:usize },

    // We can match on multiple different patterns.

    // Notice that we can disable certain variants from being found

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),


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?

1 Like

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

1 Like

@semicoleon yes, I am importing them:


extern crate strum;
extern crate strum_macros;

in my file where enum is defined:

use strum_macros::{EnumString, ToString};
use std::str::FromStr;

The error is that the "from_str" method doesn't work (which is, of course, the reason I am using strum here in the first place).

         Some(s) => Color::from_str(s).unwrap(),
   |                               ^^^^^^^^ variant or associated item not found in `Color`

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?

@semicoleon cargo 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/
25 |         Some(s) => Color::from_str(s).unwrap(),
   |                               ^^^^^^^^ variant or associated item not found in `Color`
  ::: src/bin/pizza_ordering/
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...

Can you perhaps post the complete code and error message? It's hard to guess what's wrong without seeing them.

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/ that error message complains about, and gives the suggested solution?
(adding the use of FromStr inside of, to allow use of the trait's function, that's implemented in your with derive macro...)

Thanks everyone.

I think the problem was just that I needed to add this std::str::FromStr; line into multiple files.

I like to call these "phantom imports" because you have no clue what they are even doing from just reading the code. :sweat_smile:

Also, the red underline under "EnumString" never seems to go away.

I absolutely hate that rust-analyzer leaves red squiggles under things that are correct...

problem with rust-analyzer might be related to usage of "legacy" extern crate statement...
try checking e.g. Usage of `extern crate`?

1 Like

Interesting... I didn't know this was "legacy" syntax... maybe the page and README for this crate should be updated?

I changed the import in to just be like this instead of the extern syntax:

use strum;
use strum_macros;

It all still works when I run it, but the red squiggle is still there under EnumString... :thinking:

Screen Shot 2023-04-04 at 3.05.33 PM

you might have been looking on docs page of some previous/old version?
i see no references to extern crate on neither when i search for strum and open latest v0.24.1.

1 Like

I think that was it. For some reason the 0.15.0 page was the #1 hit on google. pretty confusing though, especially because there are zero examples on github or

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 that I tend to watch for to let me know I'm looking at out of date documentation, but it looks like doesn't have something similar.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.