【发布时间】:2019-08-27 15:23:07
【问题描述】:
我有一个包含结构向量的结构,例如
fn main() {
let x: Vec<Item> = Vec::new();
// assume x is filled with stuff
do_things_with(x);
}
struct Item {
value: String,
}
struct Context {
x: Vec<Item>,
}
impl Context {
fn get(&mut self, at: usize) -> Item {
self.x[at]
}
}
fn do_things_with(x: Vec<Item>) {
let mut ctx = Context{
x: x,
};
ctx.get(5);
}
我有一个 Vec 的东西,我将它传递给某个函数,该函数创建一个上下文并将传递的值存储在该结构中。然后我想看看这个 Vec 中的项目,所以我有一些辅助函数,例如'get' 将获取指定索引处的项目。
这看起来一切都很好,在 C 或任何语言中都可以,但是 Rust 抱怨:
'cannot move out of borrowed content'
对于函数“get”,我们尝试访问向量中的项目。
我在这里做错了什么?
【问题讨论】:
标签: rust borrow-checker