【发布时间】:2014-10-26 03:49:42
【问题描述】:
我有以下代码
pub struct PropertyDeclarationBlock {
pub declarations: Arc<Vec<(PropertyDeclaration, PropertyDeclarationImportance)>>
}
impl PropertyDeclarationBlock {
pub fn select_declarations(&self) -> Arc<Vec<PropertyDeclaration>> {
Arc::new(self.declarations.clone().map_in_place(|p| {
let (declaration, _) = p;
declaration
}))
}
}
我希望能够在 PropertyDeclarationBlock 上调用 .select_declarations() 并让它返回声明的克隆,但它不是 Arc Vec(PropertyDeclaration,PropertyDeclarationImportance)只是 Arc Vec PropertyDeclaration,换句话说,返回一个 PropertyDeclaration 的向量,而不是前面的元组。
前一个无法编译,因为我收到以下错误:
error: cannot move out of dereference of `&`-pointer
Arc::new(self.declarations.clone().map_in_place(|p| {
^~~~~~~~~~~~~~~~~~~~~~~~~
据我了解,由于该函数将 self 作为参数,因此它将拥有它的所有权。因为我宁愿使用 burrow self 函数,所以我使用 &。
编辑
这是实现新功能后的错误信息:
error: cannot move out of dereference of `&`-pointer
Arc::new(self.declarations.iter().map(|&(declaration, _)| declaration).collect())
^~~~~~~~~~~~~~~~~
note: attempting to move value to here (to prevent the move, use `ref declaration` or `ref mut declaration` to capture value by reference)
Arc::new(self.declarations.iter().map(|&(declaration, _)| declaration).collect())
^~~~~~~~~~~
我尝试按照错误消息的建议在声明之前应用 ref 关键字,但没有帮助。
【问题讨论】:
标签: rust