例如:
#[macro_use]
extern crate serde_derive;
extern crate toml;
#[derive(Deserialize)]
struct Entry {
foo: String,
bar: String,
}
let toml_string = r#"
[[entry]]
foo = "a0"
bar = "b0"
[[entry]]
foo = "a1"
bar = "b1"
"#;
let config: toml::value::Table<Entry> = toml::from_str(&toml_string)?;
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,并会引发有关 的意外类型参数的错误Table。
打印任意解析值会显示您拥有的结构:
let config: toml::Value = toml::from_str(&toml_string)?;
println!("{:?}", config)
Run Code Online (Sandbox Code Playgroud)
重新格式化的输出显示您有一个带有单个键的表,该表是带有键和entry的表数组:foobar
let config: toml::Value = toml::from_str(&toml_string)?;
println!("{:?}", config)
Run Code Online (Sandbox Code Playgroud)
反序列化时,需要匹配这个结构:
#[derive(Debug, Deserialize)]
struct Outer {
entry: Vec<Entry>,
}
#[derive(Debug, Deserialize)]
struct Entry {
foo: String,
bar: String,
}
Run Code Online (Sandbox Code Playgroud)
let config: Outer = toml::from_str(&toml_string)?;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5271 次 |
| 最近记录: |