Rust 1.50.0 稳定版发布,改进数组索引

栏目: 软件资讯 · 发布时间: 4年前

内容简介:Rust 1.50.0 稳定版已发布,此版本改进了数组索引、扩展了对联合字段 (union field) 的安全访问,并将其添加到标准库。 具备 const-generic 特性的数组索引 此版本为任意长度const N的数组[T; N]增加了ops::Inde...

Rust 1.50.0 稳定版已发布,此版本改进了数组索引、扩展了对联合字段 (union field) 的安全访问,并将其添加到标准库。

具备 const-generic 特性的数组索引

此版本为任意长度const N的数组[T; N]增加了ops::IndexIndexMut实现,索引操作符[]可通过内置的 compiler magic 在数组中运行。

fn second<C>(container: &C) -> &C::Output
where
    C: std::ops::Index<usize> + ?Sized,
{
    &container[1]
}

fn main() {
    let array: [i32; 3] = [1, 2, 3];
    assert_eq!(second(&array[..]), &2); // slices worked before
    assert_eq!(second(&array), &2); // now it also works directly
}

数组中定义为const类型的值支持重复

Rust 的数组可以写为列表[a, b, c]或重复形式[x; N]

fn main() {
    // This is not allowed, because `Option<Vec<i32>>` does not implement `Copy`.
    let array: [Option<Vec<i32>>; 10] = [None; 10];

    const NONE: Option<Vec<i32>> = None;
    const EMPTY: Option<Vec<i32>> = Some(Vec::new());

    // However, repeating a `const` value is allowed!
    let nones = [NONE; 10];
    let empties = [EMPTY; 10];
}

针对ManuallyDrop<T>联合字段的安全分配

Rust 1.49 支持将ManuallyDrop<T>字段添加到union,从而允许合并Drop。不过,当一个字段被赋值时,union不会丢弃旧的值,因为它们不知道哪个变体此前是有效的,所以安全的 Rust 以前只限制为仅限于Copy类型,而Copy类型从来不会Drop。当然,ManuallyDrop<T>也不需要Drop,因此 Rust 1.50 现在允许对这些字段进行安全分配。

Rust 1.50.0 还新增了 9 个稳定的函数:

详细更新说明点此查看

延伸阅读


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Zen of CSS Design

The Zen of CSS Design

Dave Shea、Molly E. Holzschlag / Peachpit Press / 2005-2-27 / USD 44.99

Proving once and for all that standards-compliant design does not equal dull design, this inspiring tome uses examples from the landmark CSS Zen Garden site as the foundation for discussions on how to......一起来看看 《The Zen of CSS Design》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具