1 extern crate ini;
2
3 use std::io::stdout;
4
5 use ini::Ini;
6
7 const CONF_FILE_NAME: &'static str = "test.ini";
8
main()9 fn main() {
10
11 let mut conf = Ini::new();
12 conf.with_section(None::<String>)
13 .set("encoding", "utf-8");
14 conf.with_section(Some("User"))
15 .set("name", "Raspberry树莓")
16 .set("value", "Pi");
17 conf.with_section(Some("Library"))
18 .set("name", "Sun Yat-sen U")
19 .set("location", "Guangzhou=world\x0ahahaha");
20
21 conf.section_mut(Some("Library"))
22 .unwrap()
23 .insert("seats".into(), "42".into());
24
25 println!("---------------------------------------");
26 println!("Writing to file {:?}\n", CONF_FILE_NAME);
27 conf.write_to(&mut stdout()).unwrap();
28
29 conf.write_to_file(CONF_FILE_NAME).unwrap();
30
31 println!("----------------------------------------");
32 println!("Reading from file {:?}", CONF_FILE_NAME);
33 let i = Ini::load_from_file(CONF_FILE_NAME).unwrap();
34
35 println!("Iterating");
36 let general_section_name = "__General__".into();
37 for (sec, prop) in i.iter() {
38 let section_name = sec.as_ref().unwrap_or(&general_section_name);
39 println!("-- Section: {:?} begins", section_name);
40 for (k, v) in prop.iter() {
41 println!("{}: {:?}", *k, *v);
42 }
43 }
44 println!("");
45
46 let section = i.section(Some("User")).unwrap();
47 println!("name={}", section.get("name").unwrap());
48 println!("conf[{}][{}]={}", "User", "name", i["User"]["name"]);
49 println!("General Section: {:?}", i.general_section());
50 }
51