Borrow problem when update version

⚓ Rust    📅 2025-09-16    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 47 days ago. The information described in this article may have changed.

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...

3 posts - 3 participants

Read full topic

🏷️ Rust_feed