2311rust,到54版本更新

1.50.0稳定版

常量泛型数组索引

继续稳定常量泛型迈进,此版本为[T;N]数组,添加了ops::Index和IndexMut的实现.

cpp 复制代码
fn second<C>(container: &C) -> &C::Output
where
    C: std::ops::Index<usize> + ?Sized,
{
    &container[1]
}
fn main() {
    let array: [i32; 3] = [1, 2, 3];
    assert_eq!(second(&array[..]), &2); //切片以前工作,
    assert_eq!(second(&array), &2); //现在也可直接工作
}

数组的常量值重复

可按[a,b,c]列表或[x;N]重写Rust中的数组.对N>1,只允许对带Copyx重复,且允许在那里使用const式.

Rust1.38以来,实现意外地允许在数组重复中稳定地使用const值.

cpp 复制代码
fn main() {
    //禁止的,因为`'Option<Vec<i32>>'`没有实现`'Copy'`.
    let array: [Option<Vec<i32>>; 10] = [None; 10];
    const NONE: Option<Vec<i32>> = None;
    const EMPTY: Option<Vec<i32>> = Some(Vec::new());
    //但是,允许重复`"const"`值!
    let nones = [NONE; 10];
    let empties = [EMPTY; 10];
}

Rust1.50中,正式承认了该稳定性.

安全分配ManuallyDrop<T>联字段

赋值字段时,不会删除旧值,因此Rust以前将其限制为仅从不析构(Drop)复制类型.当然,ManuallyDrop<T>也不需要Drop,所以现在Rust1.50允许安全地给这些字段赋值.

Unix平台上文件的利基市场

Rust1.28中,引入了非零整数类型(如NonZeroU8),其中0是利基,允许Option<NonZero>不用额外内存0表示None.

Unix平台上,Rust文件只是由系统的整数文件描述符组成,这里它永远不可能是-1!
返回文件描述符系统调用,使用-1来指示错误(checkerrno),因此-1不可能是真正的文件描述符.

Rust1.50开始,可用它来优化布局.因此,Option<File>现在大小与File自身相同!

更改库

Rust1.50.0中,有9个新的稳定函数:

cpp 复制代码
bool::then
btree_map::Entry::or_insert_with_key
f32::clamp
f64::clamp
hash_map::Entry::or_insert_with_key
Ord::clamp
RefCell::take
slice::fill
UnsafeCell::get_mut

且大量现有函数常量:

cpp 复制代码
IpAddr::is_ipv4
IpAddr::is_ipv6
Layout::size
Layout::align
Layout::from_size_align

所有整数类型的PoW.

1,对整,checked_pow.

2,对整,saturating_pow.

3,对整,wrapping_pow.

4,对正,next_power_of_two.

5,对正,checked_power_of_two.

1.51.0稳定版

Const泛型MVP

此版本之前,Rust允许你在生命期或类型中参数化类型.如,如果想要有个通用结构数组,可编写以下内容:

cpp 复制代码
struct FixedArray<T> {
              //^^^定义
    list: [T; 32]
        //^使用.
}

随后使用FixedArray<u8>,编译器生成FixedArray单态版本,如下:

cpp 复制代码
struct FixedArray<u8> {
    list: [u8; 32]
}

数组中最为明显,有了1.51.0,你可编写对整数,布尔值或符类型的值泛型代码!(使用structenum值仍不稳定).

看看定义,及用法.

cpp 复制代码
struct Array<T, const LENGTH: usize> {
               //^^^^^^^^^^^^^^^^^^^常泛型定义.
    list: [T; LENGTH]
            //^^^^^^在此使用它.
}

现在,如果使用Array<u8,32>,编译器生成Array单态版本,如下:

cpp 复制代码
struct Array<u8, 32> {
    list: [u8; 32]
}

更多

稳定array::IntoIter

作为泛型稳定的一部分,还在稳定使用它的新API,即std::array::IntoIter.IntoIter允许在数组上创建按值迭代器.

以前,不方便遍历数组的拥有值,只能引用它们.

cpp 复制代码
fn main() {
  let array = [1, 2, 3, 4, 5];
  //以前
  for item in array.iter().copied() {
      println!("{}", item);
  }
  //现在
  for item in std::array::IntoIter::new(array) {
      println!("{}", item);
  }
}

注意,这是按独立方法添加的,而不是数组上的.into_iter(),因为这会引入一定程度的破坏;目前.into_iter()是指切片引用迭代器.

Cargo的新功能解析器

Cargo.toml中有个新的解析器选项,可在其中设置resolver="2"来告诉cargo试一个新的方法来解析功能.

开发,主机,目标依赖项,细节

cpp 复制代码
[package]
resolver = "2"
//或工作区.
[workspace]
resolver = "2"

拆分调试信息

cpp 复制代码
[profile.dev]
split-debuginfo = "unpacked"

稳定api

总之,该版本稳定了18种新方法,适合各种类型,如slicePeekable.注意ptr::addr_of!ptr::addr_of_mut!稳定性,它允许你创建指向未对齐字段原始指针.

两个宏允许安全创建未对齐的指针.

cpp 复制代码
use std::ptr;
#[repr(packed)]
struct Packed {
    f1: u8,
    f2: u16,
}
let packed = Packed { f1: 1, f2: 2 };
 //`'&packed.f2'`创建未对齐的引用,因此是未定义行为!
let raw_f2 = ptr::addr_of!(packed.f2);
assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);

以下方法已稳定.

cpp 复制代码
Arc::decrement_strong_count
Arc::increment_strong_count
Once::call_once_force
Peekable::next_if_eq
Peekable::next_if
Seek::stream_position
array::IntoIter
panic::panic_any
ptr::addr_of!
ptr::addr_of_mut!
slice::fill_with
slice::split_inclusive_mut
slice::split_inclusive
slice::strip_prefix
slice::strip_suffix
str::split_inclusive
sync::OnceState
task::Wake

1.52.0稳定版

以前,先运行cargo check,然后运行cargo clippy不会运行Clippy:Cargo中的构建缓存不会区分两者.但是,在1.52中,已修复此问题.

已稳定以下方法.

cpp 复制代码
Arguments::as_str
char::MAX
char::REPLACEMENT_CHARACTER
char::UNICODE_VERSION
char::decode_utf16
char::from_digit
char::from_u32_unchecked
char::from_u32
slice::partition_point
str::rsplit_once
str::split_once

以前稳定API现在是.

cpp 复制代码
char::len_utf8
char::len_utf16
char::to_ascii_uppercase
char::to_ascii_lowercase
char::eq_ignore_ascii_case
u8::to_ascii_uppercase
u8::to_ascii_lowercase
u8::eq_ignore_ascii_case

1.53.0稳定版

数组的IntoIterator

这是数组实现IntoIterator特征的第一个Rust版本.现在可按值遍历数组:

cpp 复制代码
for i in [1, 2, 3] {
    ..
}

以前,只能用&[1,2,3][1,2,3].iter()引用来实现.

同样,你现在可把数组传递给需要T:IntoIterator的方法:

cpp 复制代码
let set = BTreeSet::from_iter([1, 2, 3]);
for (a, b) in some_iterator.chain([1]).zip([1, 2, 3]) {
    ..
}

或模式

模式语法已在任意位置支持嵌套|.这样可编写Some(1|2)而不是Some(1) | Some(2).

cpp 复制代码
match result {
     Ok(Some(1 | 2)) => { .. }
     Err(MyError { kind: FileNotFound | PermissionDenied, .. }) => { .. }
     _ => { .. }
}

Unicode标识

标识现在可包含非ASCII符.现在可用UAX#31中定义的Unicode中的所有有效标识.包括许多不同脚本和语言的角色,但不包括表情符号.

如:

cpp 复制代码
const BL HAJ: &str = "  ";
struct 人 {
    名字: String,
}
let α = 1;

Cargo中的HEAD分支名支持

Cargo不再假定git仓库的默认HEAD名为master.

已稳定以下方法和特征实现.

cpp 复制代码
array::from_ref
array::from_mut
AtomicBool::fetch_update
AtomicPtr::fetch_update
BTreeSet::retain
BTreeMap::retain
BufReader::seek_relative
cmp::min_by
cmp::min_by_key
cmp::max_by
cmp::max_by_key
DebugStruct::finish_non_exhaustive
Duration::ZERO
Duration::MAX
Duration::is_zero
Duration::saturating_add
Duration::saturating_sub
Duration::saturating_mul
f32::is_subnormal
f64::is_subnormal
IntoIterator for array
{integer}::BITS
io::Error::Unsupported
NonZero*::leading_zeros
NonZero*::trailing_zeros
Option::insert
Ordering::is_eq
Ordering::is_ne
Ordering::is_lt
Ordering::is_gt
Ordering::is_le
Ordering::is_ge
OsStr::make_ascii_lowercase
OsStr::make_ascii_uppercase
OsStr::to_ascii_lowercase
OsStr::to_ascii_uppercase
OsStr::is_ascii
OsStr::eq_ignore_ascii_case
Peekable::peek_mut
Rc::increment_strong_count
Rc::decrement_strong_count
slice::IterMut::as_slice
AsRef<[T]> for slice::IterMut
impl SliceIndex for (Bound<usize>, Bound<usize>)
Vec::extend_from_within
相关推荐
姜学迁2 小时前
Rust-枚举
开发语言·后端·rust
凌云行者2 小时前
rust的迭代器方法——collect
开发语言·rust
QMCY_jason8 小时前
Ubuntu 安装RUST
linux·ubuntu·rust
碳苯12 小时前
【rCore OS 开源操作系统】Rust 枚举与模式匹配
开发语言·人工智能·后端·rust·操作系统·os
zaim115 小时前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
凌云行者1 天前
使用rust写一个Web服务器——单线程版本
服务器·前端·rust
cyz1410011 天前
vue3+vite@4+ts+elementplus创建项目详解
开发语言·后端·rust
超人不怕冷1 天前
[rust]多线程通信之通道
rust
逢生博客1 天前
Rust 语言开发 ESP32C3 并在 Wokwi 电子模拟器上运行(esp-hal 非标准库、LCD1602、I2C)
开发语言·后端·嵌入式硬件·rust
Maer091 天前
WSL (Linux)配置 Rust 开发调试环境
linux·运维·rust