内容简介: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 已处于稳定阶段:
Arc::as_ptrBTreeMap::remove_entryRc::as_ptrrc::Weak::as_ptrrc::Weak::from_rawrc::Weak::into_rawstr::strip_prefixstr::strip_suffixsync::Weak::as_ptrsync::Weak::from_rawsync::Weak::into_rawchar::UNICODE_VERSIONSpan::resolved_atSpan::located_atSpan::mixed_siteunix::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
以上所述就是小编给大家介绍的《Rust 1.45.0 发布》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to the Analysis of Algorithms
Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99
This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!