Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching String: cannot move out of borrowed content

Tags:

rust

req.url.fragment is an optional String. If it has a value, I want to copy that value into fragment, otherwise I want to assign an empty string. I keep getting the error that I cannot move out of borrowed content.

How do I resolve this?

fn fb_token(req: &mut Request) -> IronResult<Response> {
    let fragment = match req.url.fragment {
        Some(fragment) => fragment,
        None => "".to_string(),
    };

    Ok(Response::with((status::Ok, fragment)))
}
like image 529
W.K.S Avatar asked Apr 28 '15 18:04

W.K.S


2 Answers

It depends on what you want to do with the existing string in the structure.

let fragment = match req.url.fragment {
    Some(fragment) => fragment,
    None => "".to_string(),
};

In this code, you are moving the String out of req.url.fragment, but that would leave it in an undefined state. That's a bad thing, and Rust prevents you from doing that!

As the error message states:

to prevent the move, use ref fragment or ref mut fragment to capture value by reference

If you want to leave the string where it is and return a copy, then you can take a reference and then clone it:

let fragment = match req.url {
    Some(ref fragment) => fragment.clone(),
    None => "".to_string()
};

If you want to leave the existing string as a None, then you can use take:

let fragment = match req.url.take() {
    Some(fragment) => fragment,
    None => "".to_string()
};

Even shorter, you can use unwrap_or_else:

let fragment = req.url.take().unwrap_or_else(String::new);
like image 172
Shepmaster Avatar answered Sep 26 '22 15:09

Shepmaster


The problem here is that one cannot invalidate a &mut reference, and moving ownership out is one way to invalidate. (There's a few links to similar questions/answers in the sidebar on the right.)

A fix is to instead use references, which should work quite well in this case:

fn fb_token(req: &mut Request) -> IronResult<Response> {
    let fragment: &str = match req.url.fragment {
        Some(ref fragment) => &**fragment,
        None => "",
    };

    Ok(Response::with((status::Ok, fragment)))
}

(The &str annotation isn't necessary, just to make it clearer what's happening.)

This works because iron implements Modifier<Response> for &str, so that type can be used with with instead of the owned String.

like image 25
huon Avatar answered Sep 26 '22 15:09

huon