Rust 1.28 发布,系统内存分配器可自由选择

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

内容简介:Rust 1.28 发布了,Rust 是一种专注于安全性、速度和并发性的系统编程语言。 1.28 版本值得关注的几个点是: 分配器可控 改进了格式化的错误消息 NonZero 数字类型的稳定性 Cargo 特性变更 分配器可控 分配器是 ...

Rust 1.28 发布了,Rust 是一种专注于安全性、速度和并发性的系统编程语言。

1.28 版本值得关注的几个点是:

  • 分配器可控

  • 改进了格式化的错误消息

  • NonZero 数字类型的稳定性

  • Cargo 特性变更

分配器可控

分配器是 Rust 中的程序在运行时从系统获取内存的方式。之前 Rust 不允许改变获取内存的方式,这会导致一些问题,在某些平台上 Rust 程序的默认分配器是 jemalloc,而在其它平台则是使用系统分配器,而用户对此无法进行选择。在1.28 中 #[global_allocator] 属性现在是稳定的,这允许 Rust 程序将其分配器设置为系统分配器,以及通过实现 GlobalAlloc 特性来定义新的分配器。

用法如下:

use std::alloc::System;

#[global_allocator]
static GLOBAL: System = System;

fn main() {
    let mut v = Vec::new();
    // This will allocate memory using the system allocator.
    v.push(1);
}

    改进了格式化的错误消息

    以下边这个语句为例:

    format!("{_foo}", _foo = 6usize);

    以往,这里发出的错误消息相对简单:

    error: invalid format string: expected `'}'`, found `'_'`
      |
    2 |     format!("{_foo}", _foo = 6usize);
      |             ^^^^^^^^

    现在会发出一个诊断信息,显示格式字符串无效的具体原因:

    error: invalid format string: invalid argument name `_foo`
      |
    2 |     let _ = format!("{_foo}", _foo = 6usize);
      |                       ^^^^ invalid argument name in format string
      |
      = note: argument names cannot start with an underscore

    NonZero 数字类型的稳定性

    除了 GlobalAlloc 特性的稳定性,另一个重要的稳定性是 NonZero 数字类型。这些是标准无符号整数类型的封装:NonZeroU8、NonZeroU16、NonZeroU32、NonZeroU64、NonZeroU128 和 NonZeroUsize。

    这允许进行大小调整,例如 Option <u8> 是 2 个字节,但 Option <NonZeroU8> 只有 1 个字节。需要注意的是,即使 NonZeroU8 包含在另一个结构中,此调整仍然有效。下面的示例说明尽管将 Door 放在 Option 中,但它仍然是 1 字节。

    use std::mem;
    use std::num::NonZeroU8;
    
    struct Key(NonZeroU8);
    
    struct Door {
        key: Key,
    }
    
    fn main() {
        assert_eq!(mem::size_of::<Door>(), 1);
        assert_eq!(mem::size_of::<Option<Door>>(), 1);
    }

    Cargo 特性变更

    Cargo 现在不再允许使用构建脚本发布修改 src 目录的包,应该将包中的 src 目录视为不可变的。

    详情见发布公告与具体更新日志

    下载地址:


    【声明】文章转载自:开源中国社区 [http://www.oschina.net]


    以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

    查看所有标签

    猜你喜欢:

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

    Iterative Methods for Sparse Linear Systems, Second Edition

    Iterative Methods for Sparse Linear Systems, Second Edition

    Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00

    Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!

    图片转BASE64编码
    图片转BASE64编码

    在线图片转Base64编码工具

    XML 在线格式化
    XML 在线格式化

    在线 XML 格式化压缩工具

    RGB CMYK 转换工具
    RGB CMYK 转换工具

    RGB CMYK 互转工具