0
我使用toml
箱子来解析.toml
文件,该文件是这样的:迭代多维BTreeMap
config = { option = "?" }
array = [
{ key1 = value1, key2 = value2, key3 = value3, key4 = value4 },
{ key1 = value1, key2 = value2, key3 = value3, key4 = value4 }
]
我有一个parser.rs
文件,其中包含:
extern crate toml;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Read;
#[derive(Debug)]
pub struct ConfigParser<'a> {
pub file: &'a str
}
impl<'a> ConfigParser<'a> {
pub fn new(file: &'a str) -> ConfigParser {
ConfigParser { file: file }
}
pub fn parse(&self) -> Option<BTreeMap<String, toml::Value>> {
let mut config_string = String::new();
File::open(self.file).and_then(|mut f| {
f.read_to_string(&mut config_string)
}).unwrap();
return toml::Parser::new(&config_string).parse();
}
}
,并用它在我的main.rs
这样的文件:
extern crate toml;
mod parser;
fn main() {
let config = parser::ConfigParser::new("config.toml").parse().unwrap();
println!("{:?}", config)
}
其中pr整型:
{"config": Table({"option": String("?")})
我试图遍历config
像这样:
for (key, value) in config {
println!("{:?} {:?}", key, value)
}
这将产生:
"config" Table({"option": String("?")})
但这:
for (key, value) in config {
for v in value {
println!("{:?}", v)
}
}
抛出这个错误:
the trait `core::iter::Iterator` is not implemented for the type `toml::Value`