Rust 1.45.0 发布

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

内容简介:Rust 1.45.0 已发布。此版本有两个值得关注的变化:一是修复将大浮点数转换为小整数时出现的 undefined behavior 问题,官方将这种情况称为 unsoundness;二是在 expression, patterns 和 statement 中使用函数式...

Rust 1.45.0 已发布。此版本有两个值得关注的变化:一是修复将大浮点数转换为小整数时出现的 undefined behavior 问题,官方将这种情况称为 unsoundness;二是在 expression, patterns 和 statement 中使用函数式过程宏(procedural macros)的功能已处于稳定阶段。

修复浮点数转换为整数时的 unsoundness

对于此问题,官方的解决方案是为as关键字执行"saturating cast"。下面的例子解释了什么叫 saturating cast。

fn cast(x: f32) -> u8 {
    x as u8
}

fn main() {
    let too_big = 300.0;
    let too_small = -100.0;
    let nan = f32::NAN;

    println!("too_big_casted = {}", cast(too_big));
    println!("too_small_casted = {}", cast(too_small));
    println!("not_a_number_casted = {}", cast(nan));
}

上面的代码打印出

too_big_casted = 255
too_small_casted = 0
not_a_number_casted = 0

或者使用采用 unsafe 方式的新 API 进行转换:

let x: f32 = 1.0;
let y: u8 = unsafe { x.to_int_unchecked() };

稳定在 expression, patterns 和 statement 中使用函数式过程宏的功能

Rust 1.45.0 在三个新地方增加了调用过程宏的功能:

// imagine we have a procedural macro named "mac"

mac!(); // item position, this was what was stable before

// but these three are new:
fn main() {
  let expr = mac!(); // expression position

  match expr {
      mac!() => {} // pattern position
  }

  mac!(); // statement position
}

库变更

在 Rust 1.45.0 中,以下 API 已处于稳定阶段:

此外还增加了char with ranges 来迭代代码点(codepoint):

for ch in 'a'..='z' {
    print!("{}", ch);
}
println!();
// Prints "abcdefghijklmnopqrstuvwxyz"

详情查看 https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html


以上所述就是小编给大家介绍的《Rust 1.45.0 发布》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

python学习手册(原书第5版)

python学习手册(原书第5版)

马克·卢茨 / 机械工业出版社 / 2018-10 / 219

如果你想动手编写高效、高质量并且很容易与其他语言和工具集成的代码,本书将快速地帮助你利用Python提高效率。本书基于Python专家的流程培训课程编写,内容通俗易懂。本书包含很多注释的例子和插图,以帮助你开始使用Python2.7和3.3。每章都包含关于Python语言的重要组成部分的一节课。本书主要内容:了解Python的主要内置对象类型,如数字、列表和字典;创建和处理对象的Python语句,......一起来看看 《python学习手册(原书第5版)》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具