[][src]Derive Macro num_derive::FromPrimitive

#[derive(FromPrimitive)]

Derives num_traits::FromPrimitive for simple enums and newtypes.

Examples

Simple enums can be derived:


#[derive(FromPrimitive)]
enum Color {
    Red,
    Blue,
    Green = 42,
}

Enums that contain data are not allowed:

This example deliberately fails to compile

#[derive(FromPrimitive)]
enum Color {
    Rgb(u8, u8, u8),
    Hsv(u8, u8, u8),
}

Structs are not allowed:

This example deliberately fails to compile
#[derive(FromPrimitive)]
struct Color {
    r: u8,
    g: u8,
    b: u8,
}