1.80.0稳定版
LazyCell和LazyLock
这些"懒"类型会延迟初化其数据,直到第一次访问.它们类似1.70中稳定的OnceCell和OnceLock类型,但单元中包含初化函数.
这稳定化了从流行的lazy_static和once_cell中进入标准库.
LazyLock是线安选项,使其适合静态值等位置.如,产生线程和主域都会看见如下完全相同持续时间,因为无论谁最终先访问静,都会初化一次LAZY_TIME.
与用OnceLock::get_or_init()不同,这两个用户都不必知道如何初化它.
cpp
use std::sync::LazyLock;
use std::time::Instant;
static LAZY_TIME: LazyLock<Instant> = LazyLock::new(Instant::now);
fn main() {
let start = Instant::now();
std::thread::scope(|s| {
s.spawn(|| {
println!("Thread lazy time is {:?}", LAZY_TIME.duration_since(start));
});
println!("Main lazy time is {:?}", LAZY_TIME.duration_since(start));
});
}
LazyCell在不同步线程时,也是一样的,所以它没有实现静需要的同步,但仍可在thread_local!静中使用它(每线程有不同初化).
根据线安需求,也可在其他数据结构中用这两个类,因此懒初化无处不在!
检查配置名和值
在1.79中,rustc稳定了一个--check-cfg标志,现在Cargo1.80对它知道的所有配置名和值(除了rustc中周知名和值),都允许这些检查.
这包括Cargo.toml中的函数名及构建脚本的新的cargo::rustc-check-cfg输出.
由默认警告的抓拼写错误或其他错误配置的unexpected_cfgs检查器报告意外的配置.如,在有可选rayon依赖的项目中,为错误的特征值配置此代码:
cpp
fn main() {
println!("Hello, world!");
#[cfg(feature = "crayon")]
rayon::join(
|| println!("Hello, Thing One!"),
|| println!("Hello, Thing Two!"),
);
}
警告:意外的"配置"条件值:"Crayon".
Cargo.toml清单中的[lints]表也可用来扩展自定义配置的已知名和值列表.rustc会自动提供要在警告中使用的语法.
cpp
[lints.rust]
unexpected_cfgs = { level = "warn", checkcfg = ['cfg(foo, values("bar"))'] }
模式中的独占区间
Rust区间模式现在可用独占端点,写成类似Range和RangeTo式类型的a..b或..b.如,以下模式现在可对一个模式的结束和下个模式的开始使用相同常数:
cpp
pub fn size_prefix(n: u32) -> &'static str {
const K: u32 = 10u32.pow(3);
const M: u32 = 10u32.pow(6);
const G: u32 = 10u32.pow(9);
match n {
..K => "",
K..M => "k",
M..G => "M",
G.. => "G",
}
}
稳定的API
cpp
impl Default for Rc<CStr>
impl Default for Rc<str>
impl Default for Rc<[T]>
impl Default for Arc<str>
impl Default for Arc<CStr>
impl Default for Arc<[T]>
impl IntoIterator for Box<[T]>
impl FromIterator<String> for Box<str>
impl FromIterator<char> for Box<str>
LazyCell
LazyLock
Duration::div_duration_f32
Duration::div_duration_f64
Option::take_if
Seek::seek_relative
BinaryHeap::as_slice
NonNull::offset
NonNull::byte_offset
NonNull::add
NonNull::byte_add
NonNull::sub
NonNull::byte_sub
NonNull::offset_from
NonNull::byte_offset_from
NonNull::read
NonNull::read_volatile
NonNull::read_unaligned
NonNull::write
NonNull::write_volatile
NonNull::write_unaligned
NonNull::write_bytes
NonNull::copy_to
NonNull::copy_to_nonoverlapping
NonNull::copy_from
NonNull::copy_from_nonoverlapping
NonNull::replace
NonNull::swap
NonNull::drop_in_place
NonNull::align_offset
<[T]>::split_at_checked
<[T]>::split_at_mut_checked
str::split_at_checked
str::split_at_mut_checked
str::trim_ascii
str::trim_ascii_start
str::trim_ascii_end
<[u8]>::trim_ascii
<[u8]>::trim_ascii_start
<[u8]>::trim_ascii_end
Ipv4Addr::BITS
Ipv4Addr::to_bits
Ipv4Addr::from_bits
Ipv6Addr::BITS
Ipv6Addr::to_bits
Ipv6Addr::from_bits
Vec::<[T; N]>::into_flattened
<[[T; N]]>::as_flattened
<[[T; N]]>::as_flattened_mut
稳定的常API
cpp
<[T]>::last_chunk
BinaryHeap::new