Borrow problem when update version

Hi,

I am updating version and edition of an old project that uses to work.
All is now ok but this part of code... (If I comment this part, the other code works).

So, I can't clone some part of code, So I do not really know how to do in this case...

I have a API and this argument to deal with :

use actix_multipart::{Field, Multipart};
...

  payload: &mut Multipart,

And I have a function that parse this payload and write some file in a place...

...
 let uploaded_file_info = if let Ok(Some(mut field)) = payload.try_next().await {
        let content_disposition = field
            .content_disposition()
            .ok_or(UploadFileError::MustHaveContentDisposition)?;

        let filename = content_disposition
            .get_filename()
            .ok_or(UploadFileError::MustHaveFilename)?;

        let path = build_path(filename)?;
        let path_buf = path
            .absolutize()
            .map_err(UploadFileError::UnableToAbsolutizePathDir)?
            .to_path_buf();

      ...

        let extension = PathBuf::from(filename)
            .extension()
            .and_then(|v| v.to_str())
            .map(|v| v.to_lowercase());

        let tempfile = match extension.as_deref() {
            Some(ext) if "ps" == ext || "tar" == ext => {
                store_gzip_file(&mut field, &path_buf, ext).await?
            }

            _ => store_file(&mut field, &path_buf).await?,
        };

        UploadedFileInfo {
            filename: filename.to_string(),
            extension: extension.unwrap_or_default(),
            tempfile,
        }

And I got this error :

error[E0502]: cannot borrow `field` as mutable because it is also borrowed as immutable
   --> api\rest_file\src\upload_file.rs:136:33
    |
96  |         let content_disposition = field
    |                                   ----- immutable borrow occurs here
...
136 |                 store_gzip_file(&mut field, &path_buf, ext).await?
    |                                 ^^^^^^^^^^ mutable borrow occurs here
...
143 |             filename: filename.clone().to_string(),
    |                       -------- immutable borrow later used here

The fact is that I can't clone "field" value... So I wonder how to do in this kind of case ?
I pass from edition 2018 to 2024. I don't undestand why it uses to works before...

Given the information you have provided and the documentation of actix-multipart, it's hard to say how this code would have compiled successfully on an older edition.

  • You say “ I am updating version and edition” — do you mean that you are both changing the Rust edition, and changing the version of actix-multipart you are using? If so, I strongly recommend you work on those changes separately. Solve one, then the other, not both at once.
  • Please post the full source code of the function, including the signature and every line in the body. Borrow checking errors can involve everything in a function, and you haven't even shown us what is line 143.

Yeah I also don't see how this would ever have worked. You should post the complete original function.

There's probably a pretty easy fix though, by calling .to_string() on filename right when you get it, making it a String instead of a &str referring to part of field.

1 Like

Oh finally you were rigth about to_string convertion... I finally make it works !!! Thank you very much !

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.