#Borrowed Value does not live enough

6 messages · Page 1 of 1 (latest)

onyx mountain
#

I tried & and clone() but the error still persists. What's the correct syntax?

    let json_views: std::collections::HashMap<String, value::Value> = match &json_form["properties"]
    {
        value::Value::Object(omap) => omap.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
        _ => panic!("properties should be an object"),
    };

    let json_vec = Vec::from_iter(json_views.iter().clone());
    let form_signal = create_signal(cx, json_vec);


.
60  |     let json_vec = Vec::from_iter(json_views.iter().clone());
    |                                   ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
61  |     let form_signal = create_signal(cx, json_vec);
    |                       --------------------------- argument requires that `json_views` is borrowed for `'1`
...
104 | }
    | - `json_views` dropped here while still borrowed

glacial osprey
#
104 | }
    | - `json_views` dropped here while still borrowed```this kinda hints that there's more relevant context other than what you showed
Whatever youre keeping from that scope with the code you posted, return it to the outer scope with fully owned values, not borrows
hybrid fractal
#

How about one of these

let json_vec = json_views.into_iter().collect();
let json_vec = json_views.clone().into_iter().collect();
let json_vec = json_views.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
```The first one should work unless you need `json_views` later. The second and third should be mostly identical.

Also if there's nothing in between here, you can probably replace the whole thing with
```rs
let json_vec = json_form["properties"]
  .as_object()
  .expect("properties should be an object")
  .clone()
  .into_iter()
  .collect();
let form_signal = create_signal(cx, json_vec);
onyx mountain
#

ah, so I needed to collect() Thanks for the hint

hybrid fractal
#

The collect is more idiomatic, but the thing you need is to use into_iter

#

iter (by convention) creates an iterator of borrowed values, so when you clone that you just get two iterators of borrowed values. You need owned values.