简体   繁体   English

Rust 使用变量而不在线程中借用或克隆它

[英]Rust using variable without borrowing or cloning it in thread

I have a problem writing to files in actix web.我在写入 actix web 中的文件时遇到问题。 When I want to write to files I need to place that in a thread, but then it is trying to clone files and is borrowing the form, which causes a lifetime error.当我想写入文件时,我需要将它放在一个线程中,但随后它会尝试克隆文件并借用表单,这会导致终身错误。 How can I fix that.我该如何解决。 How can I use a variable within a thread without borrowing it.如何在线程中使用变量而不借用它。

for content in &form.files {
        let mut file = process_file(
            content.content_type.clone(),uuid.clone(),
        )?;
        web::block(move || file.write_all(&content.bytes))
            .await
            .map_err(ApiError::error)?
            .map_err(ApiError::error)?;
    }

You provide no actual reproduction case, no error message, and only a small part of the code, making investigation difficult as most SO readers are not psychic.您没有提供实际的复制案例,没有错误消息,并且只提供了一小部分代码,这使得调查变得困难,因为大多数 SO 读者都不是通灵者。

But I would assume the issue is this:但我认为问题是这样的:

for content in &form.files {

form.files is probably something like a vec, meaning here content is already a borrow. form.files可能类似于 vec,这意味着这里的content已经是借用了。

You need to move content out of the collection instead, assuming you cannot just move form.files itself, that means you need either some sort of draining iterator similar to Vec::drain , or you need to manually pop the items out of the collection.您需要将content移出集合,假设您不能只移动form.files本身,这意味着您需要某种类似于Vec::drain的排水迭代器,或者您需要手动将项目从集合中弹出.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM