I would like to construct a string slice that consists of the map's keys and values, concatenated and split by a character, say &
. I've managed to iterate over the map and push key=value
, however I don't know how to split the pairs by &
. I can add it in the format!
macro but then I have to .pop
the last one which is ugly.
Note that I have more than 4 keys in my map, so this should ideally be done iteratively.
use std::collections::BTreeMap;
fn main() {
let mut map: BTreeMap<&str, &str> = BTreeMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.insert("key3", "value3");
map.insert("key4", "value4");
let mut result = String::new();
for (key, value) in &map {
let kv = format!("{}={}", key, value);
result.push_str(&kv);
}
println!("{}", result);
let wanted_result = format!("key1=value1&key2=value2&key3=value3&key4=value4");
}