我有一个有许多价值观的枚举
enum Foo {
Bar = 0x00,
Baz = 0x01,
Qux = 0x02,
...
Quux = 0xFF
}
Run Code Online (Sandbox Code Playgroud)
有时我想将其中一个值的名称写入流中.我可以得出Debug做
writer.write(format!("I am {:?}", Foo::Quux).as_bytes())
Run Code Online (Sandbox Code Playgroud)
这将输出例如I am Quux.那很好,除了那个
实现这一目标的最佳方法是什么?
我正在尝试使用,enum但我需要将字符串值保存在数据库、Redis 等中。
pub enum Place {
Square = "1",
House = "2",
Office = "3",
// and so on... Garage = "4",
}
Run Code Online (Sandbox Code Playgroud)
但编译器抛出:
error[E0308]: mismatched types
|
3 | Square = "1",
| ^^^ expected `isize`, found `&str`
error[E0308]: mismatched types
|
4 | House = "2",
| ^^^ expected `isize`, found `&str`
error[E0308]: mismatched types
|
5 | Office = "3",
| ^^^ expected `isize`, found `&str`
For more information about this error, try `rustc --explain …Run Code Online (Sandbox Code Playgroud) rust ×2