我是一个 Rust 初学者,尝试通过使用它来理解这门语言。在尝试从函数返回一个数组时遇到了一些问题:
struct Widget {
thingies: ~[int]
}
impl Widget {
fn new() -> Widget {
Widget { thingies: ~[4, 8, 15, 16, 23, 42] }
}
fn somethings(&self) -> ~[int] {
self.thingies
}
}
fn main() {
let widget = Widget::new();
let wotsits = widget.somethings();
}
当然,这会导致编译失败并出现以下错误:
pointers.rs:11:8: 11:21 error: cannot move out of dereference of & pointer
pointers.rs:11 self.thingies
如果这段代码看起来有些混乱,我只是试图从一个实现的结构体中提取一个数组。所借用的指针并不重要,这只是我尝试存储数据的方式。
有什么方法可以正确地提取我的数组吗?
顺便说一下,我正在使用Rust 0.8