原文
1.83.0
稳定版
新的常
能力
此版本包括几个说明在常
环境中运行代码
可干的活的大型扩展
.这是指编译器在编译时
必须计算的所有代码
:常
和静
项的初值
,数组长度,枚举判定值
,常
模板参数及可从(constfn
)此类环境
调用的函数.
引用静
.当前,除了静项的初化器式
外,禁止常
环境引用静态项
.现已放开此限制
:
static S: i32 = 25;
const C: &i32 = &S;
但注意,在常
环境中,仍禁止读取可变
或内部可变静态值
.此外,常的最终值
不能引用可变或内部可变静态
:
static mut S: i32 = 0;
const C1: i32 = unsafe { S };
//错误:常访问可变全局内存
const C2: &i32 = unsafe { &S };
//错误:在`'常'`中遇见`可变内存引用`
这些限制确保常
仍是"常
":在执行整个程序过程
中,它们计算的值及它们按模式的含义
(可能涉及解引用)都是相同的.
也即,允许按可变或内部可变静态
的原始指针
计算常数
:
static mut S: i32 = 64;
const C: *mut i32 = &raw mut S;
可变引用和指针.
现在可在常
环境中使用可变引用
:
const fn inc(x: &mut i32) {*x += 1;
}
const C: i32 = {let mut c = 41;inc(&mut c);c
};
还支持可变原始指针和内部可变
:
use std::cell::UnsafeCell;
const C: i32 = {let c = UnsafeCell::new(41);unsafe { *c.get() += 1 };c.into_inner()
};
但是,只能在常的计算
中使用可变引用和指针
,它们不能变成常最终值
的一部分
:
const C: &mut i32 = &mut 4;
//`error[E0764]`:常的最终值中禁止使用可变引用
稳定的API
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
常
环境中稳定接口
:
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