gpt4 book ai didi

rust - 在 rust 中处理具有各种表示形式的枚举的规范方法

转载 作者:行者123 更新时间:2023-12-03 11:25:01 27 4
gpt4 key购买 nike

我在我的 Rust 程序中为每个大陆使用枚举。亚洲、欧洲等。然而,这个枚举对应于数据库定义中的一个整数,它可以从外部源构建,如果需要,可以转换为另一种“类型”。
例如

enum ContinentKind {
Asia,
Europe,
Africa,
America,
Oceania,
}

impl ContinentKind {
fn new(external_input: &str) -> Result<ContinentKind, String> {
match external_input {
"ASIA" => Ok(ContinentKind::Asia),
"EUROPE" => Ok(ContinentKind::Europe),
"AFRICA" => Ok(ContinentKind::Africa),
"AMERICA" => Ok(ContinentKind::America),
"OCEANIA" => Ok(ContinentKind::Oceania),
_ => Err("Wrong external input".to_string()),
}
}

fn id(&self) -> u32 {
match *self {
ContinentKind::Asia => 1,
ContinentKind::Europe => 2,
ContinentKind::Africa => 3,
ContinentKind::America => 4,
ContinentKind::Oceania => 5,
}
}

fn api_string(&self) -> String {
match *self {
ContinentKind::Asia => String::from("I love Asia"),
ContinentKind::Europe => String::from("I travel to Europe"),
ContinentKind::Africa => String::from("Hello Africa"),
ContinentKind::America => String::from("North and South America"),
ContinentKind::Oceania => String::from("O C E A N I A"),
}
}
}
所以我可以在大部分时间使用 ContinentKind::Asia,但我使用 id(&self) 方法来获取一个整数并保存到数据库,或者 api_string(&self) 在我的 http 服务器中返回一个字符串。
每个大陆都有一个类型、一个整数、一个外部定义和一个描述字符串。
Asia, "ASIA", 1, "I love Asia"
Europe, "EUROPE", 2 "I travel to Europe"
Africa, "AFRICA", 3, "Hello Africa"
America, "AMERICA", 4, "North and South America"
Oceania "OCEANIA", 5, "O C E A N I A"
乍一看,代码在我调用时有效,但我想知道是否有更好的方法来做到这一点,这需要更少的代码。
let my_type = ContinentKind::America;
println!("{}", my_type.id());
println!("{}", my_type.api_string());

let another_type = ContinentKind::new("AFRICA");

match another_type {
Ok(v)=> println!("{}", v.id()),
_ => println!("an error happend"),
}
我的意思是最终创建一个这样的结构:
struct Continent {
id: u32,
kind: ContinentKind,
externaL_str : String,
internal_str: String,
}
它有 2 个结构, from_id 或 from_external_str 并且具有嵌套的类型。
例如:
#[derive(Debug)]
struct Continent {
id: u32,
kind: ContinentKind,
external_str : String,
internal_str: String,
}

impl Continent {

fn from_external_string(external_input: &str) -> Result<Continent, String> {

match external_input {

"ASIA" => Ok(Continent{id: 1, kind:ContinentKind::Asia, external_str: String::from("ASIA"), internal_str:String::from("I love Asia")}),
"EUROPE" => Ok(Continent{id: 2, kind:ContinentKind::Europe, external_str: String::from("EUROPE"), internal_str:String::from("I travel to Europe")}),
"AFRICA" => Ok(Continent{id: 3, kind:ContinentKind::Africa, external_str: String::from("AFRICA"), internal_str:String::from("Hello Africa")}),
"AMERICA" => Ok(Continent{id: 4, kind:ContinentKind::America, external_str: String::from("AMERICA"), internal_str:String::from("North and South America")}),
"OCEANIA" => Ok(Continent{id: 5, kind:ContinentKind::Oceania, external_str: String::from("OCEANIA"), internal_str:String::from("O C E A N I A")}),
_ => Err("Wrong external input".to_string()),
}
}

fn from_database_id(id: u32) -> Result<Continent, String> {

match id {

1 => Ok(Continent{id: 1, kind:ContinentKind::Asia, external_str: String::from("ASIA"), internal_str:String::from("I love Asia")}),
2 => Ok(Continent{id: 2, kind:ContinentKind::Europe, external_str: String::from("EUROPE"), internal_str:String::from("I travel to Europe")}),
3 => Ok(Continent{id: 3, kind:ContinentKind::Africa, external_str: String::from("AFRICA"), internal_str:String::from("Hello Africa")}),
4 => Ok(Continent{id: 4, kind:ContinentKind::America, external_str: String::from("AMERICA"), internal_str:String::from("North and South America")}),
5 => Ok(Continent{id: 5, kind:ContinentKind::Oceania, external_str: String::from("OCEANIA"), internal_str:String::from("O C E A N I A")}),
_ => Err("Wrong external input".to_string()),
}
}
}

最佳答案

您可以稍微清理一下的一种方法是使用 lazy_static将您的值(value)观转化为静态的全局值(value)观。它的核心是这样的:

lazy_static! {
static ref ASIA: Continent = Continent::new(1, ContinentKind::Asia, "ASIA", "I love Asia");
static ref EUROPE: Continent = Continent::new(2, ContinentKind::Europe, "EUROPE", "I travel to Europe");
// ...
}
然后您可以构建您的 Continent::from_external_stringContinent::from_database_id返回对这些的引用(如果您确实需要值而不是引用,则返回副本)。
impl Continent {
fn from_external_string(external_input: &str) -> Result<&'static Continent, String> {
match external_input {
"ASIA" => Ok(&*ASIA),
"EUROPE" => Ok(&*EUROPE),
// ...
_ => Err("Wrong external input".to_string()),
}
}

fn from_database_id(id: u32) -> Result<&'static Continent, String> {
match id {
1 => Ok(&*ASIA),
2 => Ok(&*EUROPE),
// ...
_ => Err("Wrong external input".to_string()),
}
}
}
最后,如果你仍然需要它们,你可以连接枚举函数来使用它们:
impl ContinentKind {
fn to_continent(&self) -> &'static Continent {
match self {
ContinentKind::Asia => &*ASIA,
ContinentKind::Europe => &*EUROPE,
// ...
}
}
fn id(&self) -> u32 {
self.to_continent().id
}
// ...
}
注意:通常我不喜欢使用 global 来管理这种事情,但如果你真的想使用硬编码的枚举,这至少可以最大限度地减少你需要保持同步的位。

关于rust - 在 rust 中处理具有各种表示形式的枚举的规范方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62877824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com