You've got a couple issues here, and since it's not clear what Value
and substr
refer to, I'm going to explain what I see based on inferences.
First, you're returning &'static Value
, but I don't see anything here that indicates Value::get
will return a &'static Value
. If you're taking vd
in with a move, &vd
is not going to be static, the value will be dropped at the end of the function call, so assigning a reference to r
to return it doesn't look right.
while lj.contains(".") {
let u = lj.find(".").unwrap();
This looks very inefficient. You're going to search the string for the same thing twice on every loop. Just do a find and exit the loop if nothing is found. Don't use contains
when it is redundant with find
.
r=r.get(substr(lj,0,u)).unwrap();
lj=substr(lj,u,o);
The actual error you're getting is from here. Apparently substr
is taking its first argument by value. This moves lj
, so it will not be available afterward. This is fixable with r.get(substr(lj.clone(),0,u))
, which makes your first call to clone
redundant. But in general, I'd be worried that substr
is wrong here, because I don't see why it'd want to take a String
by value in the first place. You can sub-string with a slice instead.
All in all, I'd expect the function to end up looking something more like this:
fn json_by_path<'a>(stre: &str, mut vd: &'a Value) -> &'a Value {
while let Some(u) = stre.find(".") {
vd = vd.get(stre[..u]).unwrap();
stre = stre[u..];
}
return vd;
}
But again, I don't know what Value
and get
are, so this is a bit of a conjecture.