Error while parsing String

Hello,
I have this code:

fn json_by_path(stre: String,vd: Value) -> &'static Value {
    let mut lj = stre.clone();
    let mut r: &Value = &vd;
    while lj.contains(".") {
        let o = lj.len();
        let u = lj.find(".").unwrap();
        r=r.get(substr(lj,0,u)).unwrap();
        lj=substr(lj,u,o);
    }
    return r;
}

and i have this error:

 error[E0382]: use of moved value: `lj`
   --> src/main.rs:115:19
    |
114 |         r=r.get(substr(lj,0,u)).unwrap();
    |                        -- value moved here
115 |         lj=substr(lj,u,o);
    |                   ^^ value used here after move
    |
    = note: move occurs because `lj` has type `std::string::String`, which does not implement the `Copy` trait

If you can help me.
ccgauche

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.