I don't know how convert Option<&T>
to Option<T>
. I'm using the reqwest crate. This is my code:
let body: Option<Body> = request.body();
But I get mismatched type
error. How do I convert Option<&Body>
to <Option<Body>>
?
I don't know how convert Option<&T>
to Option<T>
. I'm using the reqwest crate. This is my code:
let body: Option<Body> = request.body();
But I get mismatched type
error. How do I convert Option<&Body>
to <Option<Body>>
?
When you have an Option<&T>
, you don't actually have the object itself. You have, maybe, a reference to the said object. Obviously, you cannot go from "a reference" to "a full-fledged owned object" without some trickery in between. You only currently have a pointer to the object, not the object itself, and as such, your only recourse is to find a way to make a copy of it.
If T
implements Clone
, you could in theory clone()
your object and call it a day - this would turn your Option<&T>
into Option<T>
at the cost of a full memory copy. Judging by the type signature, this is probably a hyper
question, however, and Body
does not implement such a trait.
All other solutions will fail:
self
, as that would invalidate the borrow you have on &T
&T
to T
as you only have a borrow