`“编译错误与闭包-6ren"> `“编译错误与闭包-我正在为 Servo 进行 PR,我正在尝试确定请求 URL 是否与响应 header 中提供的 URL 列表共享来源。我正在尝试使用在 URL 列表上的折叠调用中运行的闭包来确定这一点。闭包需要使用-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:31:11 26 4
gpt4 key购买 nike

我正在为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com