如何从函数返回拥有的数组? [英] How do I return an owned array from a function?

查看:58
本文介绍了如何从函数返回拥有的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rust的新手,正试图通过玩弄它来弄清楚这门语言.我在尝试从函数返回数组时遇到了一些问题:

I'm a Rust newbie trying to figure out the language by playing with it. I've hit some problems on trying to return an array from a function:

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();
}

这当然会因以下错误而失败:

This of course fails compilation with this error:

pointers.rs:11:8: 11:21 error: cannot move out of dereference of & pointer
pointers.rs:11         self.thingies

如果此代码示例看起来有些混乱,我要做的就是从实现的结构中拉出一个数组.借用的指针并不重要,这只是我试图存储数据的方式.

In case this code sample looks out of sorts, all I'm trying to do is pull an array out of an implemented struct. The borrowed pointer isn't important, it's just how I'm trying to store the data.

关于如何正确提取数组的任何提示吗?

Any tips on how to extract my array properly?

顺便说一句,我正在使用Rust 0.8

Btw, I'm using Rust 0.8

推荐答案

代码无法编译的原因是,唯一指针~只能具有一个所有者.编译器阻止您编写易于出错的代码.您可以决定返回一个Thingies的副本,对Thingies的引用或一片Thingies(这是对矢量数据的引用或其一部分).

The reason your code doesn't compile is that a unique pointer ~ can have only one owner. The compiler is preventing you from writing error prone code. You can either decide to return a copy of thingies, a reference to thingies, or a slice of thingies (which is a reference to the vector data or a segment of it).

复制解决方案

struct Widget {
  thingies: ~[int]
}

impl Widget {
    fn new() -> Widget {
        Widget { thingies: ~[4, 8, 15, 16, 23, 42] }
    }

    fn somethings(&self) -> ~[int] {
        self.thingies.clone()
    }
}

参考解决方案

struct Widget {
  thingies: ~[int]
}

impl Widget {
    fn new() -> Widget {
        Widget { thingies: ~[4, 8, 15, 16, 23, 42] }
    }

    fn somethings<'a>(&'a self) -> &'a~[int] {
        &self.thingies
    }
}

切片解决方案

struct Widget {
  thingies: ~[int]
}

impl Widget {
    fn new() -> Widget {
        Widget { thingies: ~[4, 8, 15, 16, 23, 42] }
    }

    fn somethings<'a>(&'a self) -> &'a[int] {
        self.thingies.as_slice()
    }
}

要了解引用和切片解决方案,您需要了解'a的含义:它表示生命周期,而&'a是一种告诉编译器该引用绝不能超过其引用的对象的方法,在此情况下, case是一个小部件.

To understand the reference and slice solutions you need to understand what 'a means: it indicates a lifetime, and &'a is a way to tell the compiler that the reference must never outlive the object it references, which in this case is a Widget.

这些解决方案也有一些限制:您不能修改当前正在引用的对象,因为这样做会增加引用无效的可能性.

These solutions also have some limitations: you cannot modify an object that you're currently referencing because doing so opens up the possibility of the references becoming invalid.

如果返回可变引用,您当然可以修改thingies.具有生存期的可变引用将写为&'a mut T

You can of course modify thingies if you return a mutable reference. A mutable reference with a lifetime would be written &'a mut T

struct Widget {
  thingies: ~[int]
}

impl Widget {
    fn new() -> Widget {
        Widget { thingies: ~[4, 8, 15, 16, 23, 42] }
    }

    fn somethings<'a>(&'a mut self) -> &'a mut ~[int] {
        &mut self.thingies
    }
}

注意,我相信在Rust 0.8中,您需要编写&'self而不是&'a,因为尚不支持使用自定义名称的生存期.我也是在0.9中写的.

Note I believe that in Rust 0.8, you need to write &'self instead of &'a because lifetimes with custom names weren't supported yet. I also wrote this in 0.9.

删除了冗余生存期声明.

removed redundant lifetime declarations.

这篇关于如何从函数返回拥有的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆