2

In C++, I can define an enum class where constant char values are given to different fields:

enum class BasicOperators : char {
    plus='+',
    minus='-',
    mul='*',
    div='/'
}

From Enums with constant values in Rust, I am aware that I can define an enum with an integer:

enum MyEnum {
    A = 123,
    b = 456,
}

If I define an enum with char values like this:

enum BasicOperators {
    Plus = '+',
    Minus = '-',
    Mul = '*',
    Div = '/',
}

the compiler complains:

error[E0308]: mismatched types
 --> src/lib.rs:2:12
  |
2 |     Plus = '+',
  |            ^^^ expected isize, found char
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
0x00A5
  • 1,462
  • 1
  • 16
  • 20
  • 1
    Note that the closest approximation of the `char` type in C++ is the `u8` type in Rust. (The types are not completely equivalent, because in C++ it is not defined whether `char` is signed, nor does it always have 8 bits, but it's close enough in this context.) The closest equivalent of the Rust type `char` in C++ is `char32_t`. – Sven Marnach Jan 09 '19 at 21:55

0 Answers0