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)))
}
It depends on what you want to do with the existing string in the structure.
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:
If you want to leave the string where it is and return a copy, then you can take a reference and then clone it:
If you want to leave the existing string as a
None
, then you can usetake
:Even shorter, you can use
unwrap_or_else
: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:
(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 withwith
instead of the ownedString
.