Rust 1.45.0 更新了

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 已处于稳定阶段:

Arc::as_ptr

BTreeMap::remove_entry

Rc::as_ptr

rc::Weak::as_ptr

rc::Weak::from_raw

rc::Weak::into_raw

str::strip_prefix

str::strip_suffix

sync::Weak::as_ptr

sync::Weak::from_raw

sync::Weak::into_raw

char::UNICODE_VERSION

Span::resolved_at

Span::located_at

Span::mixed_site

unix::process::CommandExt::arg0

此外还增加了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

  • A+
所属分类:it杂谈 时间:2020-07-18