我是一名初学者,之前只接触过JS/TS,现在想学习Rust。
我已经看到了其他类似的问题,例如:如何在Rust中迭代结构体的元素? 但是它们没有给我一个真正的答案。
我想在Rust中遍历结构体的键和值。
在JS/TS中,可以这样实现:
const o = {
a: "hello",
b: "world"
};
const keys = Object.keys(o);
const values = Object.values(o);
// now loop over them
这样的东西在Rust中该如何实现?
我正在使用Serde将配置yaml文件解析成结构体。
#[derive(Deserialize, Debug, Clone)]
pub struct Config {
pub headers: Headers,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Headers {
#[serde(rename = "Content-Security-Policy")]
pub content_security_policy: String,
#[serde(rename = "x-frame-options")]
pub x_frame_options: String,
#[serde(rename = "x-content-type-options")]
pub x_content_type_options: String,
#[serde(rename = "x-permitted-cross-domain-policies")]
pub x_permitted_cross_domain_policies: String,
#[serde(rename = "x-download-options")]
pub x_download_options: String,
#[serde(rename = "x-xss-protection")]
pub x_xss_protection: String,
#[serde(rename = "referrer-policy")]
pub referrer_policy: String,
#[serde(rename = "Strict-Transport-Security")]
pub strict_transport_security: String,
#[serde(rename = "feature-policy")]
pub feature_policy: String,
#[serde(rename = "Cache-Control")]
pub cache_control: String,
}
但是这并没有实现
.iter()
函数,我在搜索中也没有找到解决方案。
HashMap<String, String>
。请参见这里。 - Caesar