2411rust,1.83

原文

1.83.0稳定版

新的常能力

此版本包括几个说明在常环境中运行代码可干的活的大型扩展.这是指编译器在编译时必须计算的所有代码:常和静项的初值,数组长度,枚举判定值,常模板参数及可从(constfn)此类环境调用的函数.

引用静.当前,除了静项的初化器式外,禁止常环境引用静态项.现已放开此限制:

cpp 复制代码
static S: i32 = 25;
const C: &i32 = &S;

但注意,在常环境中,仍禁止读取可变或内部可变静态值.此外,常的最终值不能引用可变或内部可变静态:

cpp 复制代码
static mut S: i32 = 0;
const C1: i32 = unsafe { S };
//错误:常访问可变全局内存
const C2: &i32 = unsafe { &S };
//错误:在`'常'`中遇见`可变内存引用`

这些限制确保常仍是"常":在执行整个程序过程中,它们计算的值及它们按模式的含义(可能涉及解引用)都是相同的.

也即,允许按可变或内部可变静态的原始指针计算常数:

cpp 复制代码
static mut S: i32 = 64;
const C: *mut i32 = &raw mut S;

可变引用和指针.

现在可在常环境中使用可变引用:

cpp 复制代码
const fn inc(x: &mut i32) {
    *x += 1;
}
const C: i32 = {
    let mut c = 41;
    inc(&mut c);
    c
};

还支持可变原始指针和内部可变:

cpp 复制代码
use std::cell::UnsafeCell;
const C: i32 = {
    let c = UnsafeCell::new(41);
    unsafe { *c.get() += 1 };
    c.into_inner()
};

但是,只能在常的计算中使用可变引用和指针,它们不能变成常最终值的一部分:

cpp 复制代码
const C: &mut i32 = &mut 4;
//`error[E0764]`:常的最终值中禁止使用可变引用

稳定的API

cpp 复制代码
BufRead::skip_until
ControlFlow::break_value
ControlFlow::continue_value
ControlFlow::map_break
ControlFlow::map_continue
DebugList::finish_non_exhaustive
DebugMap::finish_non_exhaustive
DebugSet::finish_non_exhaustive
DebugTuple::finish_non_exhaustive
ErrorKind::ArgumentListTooLong
ErrorKind::Deadlock
ErrorKind::DirectoryNotEmpty
ErrorKind::ExecutableFileBusy
ErrorKind::FileTooLarge
ErrorKind::HostUnreachable
ErrorKind::IsADirectory
ErrorKind::NetworkDown
ErrorKind::NetworkUnreachable
ErrorKind::NotADirectory
ErrorKind::NotSeekable
ErrorKind::ReadOnlyFilesystem
ErrorKind::ResourceBusy
ErrorKind::StaleNetworkFileHandle
ErrorKind::StorageFull
ErrorKind::TooManyLinks
Option::get_or_insert_default
Waker::data
Waker::new
Waker::vtable
char::MIN
hash_map::Entry::insert_entry
hash_map::VacantEntry::insert_entry

常环境中稳定接口:

cpp 复制代码
Cell::into_inner
Duration::as_secs_f32
Duration::as_secs_f64
Duration::div_duration_f32
Duration::div_duration_f64
MaybeUninit::as_mut_ptr
NonNull::as_mut
NonNull::copy_from
NonNull::copy_from_nonoverlapping
NonNull::copy_to
NonNull::copy_to_nonoverlapping
NonNull::slice_from_raw_parts
NonNull::write
NonNull::write_bytes
NonNull::write_unaligned
OnceCell::into_inner
Option::as_mut
Option::expect
Option::replace
Option::take
Option::unwrap
Option::unwrap_unchecked
Option::<&_>::copied
Option::<&mut _>::copied
Option::<Option<_>>::flatten
Option::<Result<_, _>>::transpose
RefCell::into_inner
Result::as_mut
Result::<&_, _>::copied
Result::<&mut _, _>::copied
Result::<Option<_>, _>::transpose
UnsafeCell::get_mut
UnsafeCell::into_inner
array::from_mut
char::encode_utf8
{float}::classify
{float}::is_finite
{float}::is_infinite
{float}::is_nan
{float}::is_normal
{float}::is_sign_negative
{float}::is_sign_positive
{float}::is_subnormal
{float}::from_bits
{float}::from_be_bytes
{float}::from_le_bytes
{float}::from_ne_bytes
{float}::to_bits
{float}::to_be_bytes
{float}::to_le_bytes
{float}::to_ne_bytes
mem::replace
ptr::replace
ptr::slice_from_raw_parts_mut
ptr::write
ptr::write_unaligned
<*const _>::copy_to
<*const _>::copy_to_nonoverlapping
<*mut _>::copy_from
<*mut _>::copy_from_nonoverlapping
<*mut _>::copy_to
<*mut _>::copy_to_nonoverlapping
<*mut _>::write
<*mut _>::write_bytes
<*mut _>::write_unaligned
slice::from_mut
slice::from_raw_parts_mut
<[_]>::first_mut
<[_]>::last_mut
<[_]>::first_chunk_mut
<[_]>::last_chunk_mut
<[_]>::split_at_mut
<[_]>::split_at_mut_checked
<[_]>::split_at_mut_unchecked
<[_]>::split_first_mut
<[_]>::split_last_mut
<[_]>::split_first_chunk_mut
<[_]>::split_last_chunk_mut
str::as_bytes_mut
str::as_mut_ptr
str::from_utf8_unchecked_mut
相关推荐
wflynn1 天前
GitHub 今日推荐|rust-anything:录制真实输入输出,渐进式重写热点函数到 Rust
rust·开源·github·cli·claude-code·agent-skills
风笙9782 天前
[Rust] String 与 &str 详解
rust
风笙9782 天前
[Rust]复合类型
rust
风笙9782 天前
[Rust] 所有权与引用
rust
Kapaseker2 天前
最近用 Rust 写工具,我发现这 7 个库是真好用
rust
SomeOtherTime3 天前
第 11 讲(大结局):编译期元编程与零拷贝架构——卫生声明宏(macro_rules!)、过程宏 AST 手术刀与 Serde 零反射序列化的终极合龙
rust
SomeOtherTime3 天前
番外篇(第 12 讲):走向工业界深水区——从物理定律看 Rust 在真实工程中的“降维打击”与“阿喀琉斯之踵”
开发语言·rust
Kapaseker3 天前
Rust 桌面开发到底怎么选?先看这篇文章!
rust
奶油话梅糖3 天前
容器启停 2.5 毫秒:11 天 1,700 Star 的 Kiri,用一个 900 KB 的 PID 1 干掉了 runc 和 dockerd
前端·人工智能·rust
wflynn3 天前
GitHub 今日推荐|kiri:clone3 系统调用直达容器,2.5 毫秒启动无 runc 中间层
docker·rust·kubernetes·开源·github·containers