I've got the following code snippet which is part of a bigger function and I have borrow checker complaining for (obviously valid) reason but I don't quite understand why.
async fn func() -> Result<()> {
let mut file_mime_sub_type = None;
while let Some(item) = payload.try_next().await? {
let mut field = item;
let mut content = vec![];
// Field in turn is stream of *Bytes* object
while let Some(chunk) = field.next().await {
let chunk = chunk?;
content.push(chunk);
}
let field_name = field.name();
let content_type = field.content_type().unwrap_or(&mime::TEXT_PLAIN).to_owned();
let mime_subtype = content_type.subtype();
let content = content.concat();
if mime_subtype.eq(&mime::TEXT_PLAIN.subtype()) {
} else if is_supported_media_type(mime_subtype) {
file_mime_sub_type = Some(mime_subtype);
}
}
if let Some(mime_subtype) = file_mime_sub_type {
// do something
}
...
}
The error I see is:
|
63 | let content_type = field.content_type().unwrap_or(&mime::TEXT_PLAIN).to_owned();
| ------------ binding `content_type` declared here
64 | let mime_subtype = content_type.subtype();
| ^^^^^^^^^^^^ borrowed value does not live long enough
...
79 | }
| - `content_type` dropped here while still borrowed
...
93 | if let Some(mime_subtype) = file_mime_sub_type {
| ------------------ borrow later used here
I'm struggling to understand why content_type
is a borrowed value. I did use to_owned
and then assigned it to file_mime_sub_type
. Doesn't this move the owned value to that variable?
Below I add the fn signature of a couple of function. Hopefully that helps.
pub fn content_type(&self) -> Option<&mime::Mime> {
self.ct.as_ref()
}
pub fn subtype(&self) -> Name {}
``