Expand description
§Strum
Strum is a set of macros and traits for working with enums and strings easier in Rust.
The full version of the README can be found on GitHub.
§Including Strum in Your Project
Import strum and strum_macros into your project by adding the following lines to your
Cargo.toml. strum_macros contains the macros needed to derive all the traits in Strum.
[dependencies]
strum = "0.26"
strum_macros = "0.26"
# You can also access strum_macros exports directly through strum using the "derive" feature
strum = { version = "0.26", features = ["derive"] }
Modules§
- Documentation for Additional Attributes
Enums§
- The
ParseErrorenum is a collection of all the possible reasons an enum can fail to parse from a string.
Traits§
- AsStaticRefDeprecatedA cheap reference-to-reference conversion. Used to convert a value to a reference value with
'staticlifetime within generic code. - A trait for capturing the number of variants in Enum. This trait can be autoderived by
strum_macros. - Associates additional pieces of information with an Enum. This can be autoimplemented by deriving
EnumMessageand annotating your variants with#[strum(message="...")]. EnumPropertyis a trait that makes it possible to store additional information with enum variants. This trait is designed to be used with the macro of the same name in thestrum_macroscrate. Currently, the only string literals are supported in attributes, the other methods will be implemented as additional attribute types become stabilized.- This trait designates that an
Enumcan be iterated over. It can be auto generated usingstrum_macroson your behalf. - A trait for retrieving a static array containing all the variants in an Enum. This trait can be autoderived by
strum_macros. For derived usage, all the variants in the enumerator need to be unit-types, which means you can’t autoderive enums with inner data in one or more variants. Consider using it alongsideEnumDiscriminantsif you require inner data but still want to have an static array of variants. - A trait for retrieving the names of each variant in Enum. This trait can be autoderived by
strum_macros.
Derive Macros§
- AsRefStr
deriveConverts enum variants to&'static str. - Display
deriveConverts enum variants to strings. - EnumCount
deriveAdd a constantusizeequal to the number of variants. - EnumDiscriminants
deriveGenerate a new type with only the discriminant names. - Generated
is_*()methods for each variant. E.g.Color.is_red(). - EnumIter
deriveCreates a new type that iterates of the variants of an enum. - EnumMessage
deriveAdd a verbose message to an enum variant. - EnumProperty
deriveAdd custom properties to enum variants. - EnumString
deriveConverts strings to enum variants based on their name. - Creates a new type that maps all the variants of an enum to another generic value.
- Generated
try_as_*()methods for all tuple-style variants. E.g.Message.try_as_write(). - FromRepr
deriveAdd a function to enum that allows accessing variants by its discriminant - IntoStaticStr
deriveImplementsFrom<MyEnum> for &'static stron an enum. - VariantArray
deriveAdds a static array with all of the Enum’s variants. - VariantNames
deriveImplementsStrum::VariantNameswhich adds an associated constantVARIANTSwhich is an array of discriminant names.