I'm trying to get a deeply-nested JSON object from a vector of strings using the json crate:
fn main() {
let my_vec = ["foo", "bar", "baz", "foobar", "barfoo"];
let mut curr_obj = object!();
for i in 0..my_vec.len() {
let name = my_vec[i];
curr_obj = addObj(curr_obj, name);
}
}
fn addObj(mut obj: json::JsonValue, name: &str) -> json::JsonValue {
obj[name] = json::JsonValue::new_object();
let retob = obj[name];
retob.to_owned() // is empty but should be obj["foo"] = object!();
}
The object is an empty one here. My desired output looks like this:
{
"foo": {
"bar": {
"baz": {
"foobar": {
"barfoo": {}
}
}
}
}
}
I get the error
error[E0507]: cannot move out of indexed content
--> src/main.rs:15:17
|
15 | let retob = obj[name];
| ^^^^^^^^^
| |
| cannot move out of indexed content
| help: consider using a reference instead: `&obj[name]`
addObjis not idiomatic Rust style, it should be snake_case:add_obj.