This uses the TOML crate for the structure and serialization. The main benefit is that values should be properly escaped.
use std::fs;
use toml::{map::Map, Value}; // 0.5.1
fn to_toml(v: Vec<(String, (String, u32))>) -> Value {
let mut servers = Map::new();
for (name, (ip_addr, port)) in v {
let mut server = Map::new();
server.insert("Ipaddr".into(), Value::String(ip_addr));
server.insert("Port no".into(), Value::Integer(port as i64));
servers.insert(name, Value::Table(server));
}
let mut map = Map::new();
map.insert("server".into(), Value::Table(servers));
Value::Table(map)
}
fn main() {
let v = vec![
("A".into(), ("192.168.4.1".into(), 4476)),
("B".into(), ("192.168.4.8".into(), 1234)),
];
let toml_string = toml::to_string(&to_toml(v)).expect("Could not encode TOML value");
println!("{}", toml_string);
fs::write("servers.toml", toml_string).expect("Could not write to file!");
}
You can also use this with Serde's automatic serialization and deserialization to avoid dealing with the low-level details:
use serde::Serialize; // 1.0.91
use std::{collections::BTreeMap, fs};
use toml; // 0.5.1
#[derive(Debug, Default, Serialize)]
struct Servers<'a> {
servers: BTreeMap<&'a str, Server<'a>>,
}
#[derive(Debug, Serialize)]
struct Server<'a> {
#[serde(rename = "Ipaddr")]
ip_addr: &'a str,
#[serde(rename = "Port no")]
port_no: i64,
}
fn main() {
let mut file = Servers::default();
file.servers.insert(
"A",
Server {
ip_addr: "192.168.4.1",
port_no: 4476,
},
);
file.servers.insert(
"B",
Server {
ip_addr: "192.168.4.8",
port_no: 1234,
},
);
let toml_string = toml::to_string(&file).expect("Could not encode TOML value");
println!("{}", toml_string);
fs::write("servers.toml", toml_string).expect("Could not write to file!");
}
vector<(string,(string, u32))>
is not a valid type, at least using the standard library. If you are using types not found in the standard library, you need to include that in your question. You should produce a minimal reproducible example that will allow people to better understand your problem and help you. – Pearliepearline