rust - 如何修复 "cannot move out of dereference of ` std::cell::Ref <'_, servo_url::ServoUrl>`“编译错误与闭包

标签 rust

我正在为 Servo 进行 PR,我正在尝试确定请求 URL 是否与响应 header 中提供的 URL 列表共享来源。我正在尝试使用在 URL 列表上的折叠调用中运行的闭包来确定这一点。闭包需要使用请求 URL,但 rustc 提示请求 URL 没有复制特征。

为了解决这个问题,我尝试克隆 URL,然后将其放入 RefCell,然后从那里借用它,但现在我遇到了当前错误,我不知道如何解决它。

let url = request.current_url();
//res is the response
let cloned_url = RefCell::new(url.clone());
let req_origin_in_timing_allow = res
    .headers()
    .get_all("Timing-Allow-Origin")
    .iter()
    .map(|header_value| {
        ServoUrl::parse(header_value.to_str().unwrap())
            .unwrap()
            .into_url()
    })
    .fold(false, |acc, header_url| {
        acc || header_url.origin() == cloned_url.borrow().into_url().origin()
    });

确切的编译器错误

error[E0507]: cannot move out of dereference of `std::cell::Ref<'_, servo_url::ServoUrl>`
    --> components/net/http_loader.rs:1265:70
     |
1265 |         .fold(false, |acc, header_url| acc || header_url.origin() == cloned_url.borrow().into_url().origin());
     |                                                                      ^^^^^^^^^^^^^^^^^^^ move occurs because value has type `servo_url::ServoUrl`, which does not implement the `Copy` trait

最佳答案

into_*() 函数,例如 into_url() 按照惯例获取 self 的所有权,这意味着它们销毁(或回收)他们的输入,不留下任何东西。

使用 .borrow(),您只能看到值,但不能销毁它。

因此,要么调用 .clone() 获取您自己的副本以传递给 into_url(),或者如果您可以使用借来的值,请尝试 as_url () 即借用而不是破坏原件。

关于rust - 如何修复 "cannot move out of dereference of ` std::cell::Ref <'_, servo_url::ServoUrl>`“编译错误与闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57349512/

相关文章:

string - 为什么str主要以借用形式存在? [复制]

macros - 尝试!不会编译不匹配的类型

rust - M1 Mac 在编译 rust 代码时遇到错误

rust - 由于生命周期/借用错误,文本文件解析函数无法编译

rust - 如何解决错误 "thread ' main' paniced at 'no current reactor'”?

rust - 如何有效地将切片复制到 Rust VecDeque 中

rust - 迭代 Rust 中的命名正则表达式组

Rust MemWriter 返回指向 Buffer 的指针

rust - Java 的 BigInteger(..).longValue() 在 Rust 中的等价物是什么?

generics - 当无法命名其中一种关联类型时如何实现特征?