我試圖使用the json
crate字符串矢量得到一個深度嵌套的JSON對象:字符串轉換的矢量到deply嵌套JSON對象
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!();
}
目的是在這裏空單。我期望的輸出是這樣的:
{
"foo": {
"bar": {
"baz": {
"foobar": {
"barfoo": {}
}
}
}
}
}
我得到的錯誤
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]`
'addObj'不地道鏽的風格,應該snake_case:'add_obj'。 – Shepmaster