2411rust,1.80

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
相关推荐
mounter6259 小时前
深入理解 gccrs:GCC 如何实现 Rust 编译器,以及编译 Linux 内核面临的挑战
linux·驱动开发·rust
itmigrate1 天前
Rust 里的所有权误区:不是不写 clone 就叫高性能
开发语言·后端·rust
小灰灰搞电子2 天前
Rust+Slint 实现动态流光边框源码分享
开发语言·前端·rust·流光边框
Bigger2 天前
开源了一个"先预览、能反悔"的文件整理工具,聊聊背后的工程细节
rust·开源·app
老王IT2 天前
Rust开发环境搭建教程
开发语言·后端·rust
@大迁世界2 天前
告别 Go,拥抱 Rust
开发语言·golang·rust
低调滴开发2 天前
3 行代码写崩一个函数?切片才是 Rust 字符串处理的正确姿势
开发语言·后端·rust
小灰灰搞电子2 天前
Rust+Slint 实现高级动态消息提示框源码分享
开发语言·rust
qq_406981032 天前
大白话rust系列08 当变量遮蔽遇上引用,探究变量遮蔽的本质
开发语言·后端·rust
轻舟技术A3 天前
Pi、rpi、Pi-Bolt 重测:rpi 在启动、内存与读取上全面领先
人工智能·rust