• 2311rust,到54版本更新


    1.50.0稳定版

    常量泛型数组索引

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

    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); //现在也可直接工作
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    数组的常量值重复

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

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

    fn main() {
        //禁止的,因为`'Option>'`没有实现`'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];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    安全分配ManuallyDrop联字段

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

    Unix平台上文件的利基市场

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

    Unix平台上,Rust文件只是由系统的整数文件描述符组成,这里它永远不可能是-1!
    返回文件描述符系统调用,使用-1来指示错误(checkerrno),因此-1不可能是真正的文件描述符.
    Rust1.50开始,可用它来优化布局.因此,Option现在大小与File自身相同!

    更改库

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

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    且大量现有函数常量:

    IpAddr::is_ipv4
    IpAddr::is_ipv6
    Layout::size
    Layout::align
    Layout::from_size_align
    
    • 1
    • 2
    • 3
    • 4
    • 5

    所有整数类型的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允许你在生命期或类型中参数化类型.如,如果想要有个通用结构数组,可编写以下内容:

    struct FixedArray<T> {
                  //^^^定义
        list: [T; 32]
            //^使用.
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    struct FixedArray<u8> {
        list: [u8; 32]
    }
    
    • 1
    • 2
    • 3

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

    struct Array<T, const LENGTH: usize> {
                   //^^^^^^^^^^^^^^^^^^^常泛型定义.
        list: [T; LENGTH]
                //^^^^^^在此使用它.
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    struct Array<u8, 32> {
        list: [u8; 32]
    }
    
    • 1
    • 2
    • 3

    更多

    稳定array::IntoIter

    作为泛型稳定的一部分,还在稳定使用它的新API,即std::array::IntoIter.IntoIter允许在数组上创建按值迭代器.
    以前,不方便遍历数组的拥有值,只能引用它们.

    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);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

    Cargo的新功能解析器

    Cargo.toml中有个新的解析器选项,可在其中设置resolver="2"来告诉cargo试一个新的方法来解析功能.
    开发,主机,目标依赖项,细节

    [package]
    resolver = "2"
    //或工作区.
    [workspace]
    resolver = "2"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    拆分调试信息

    [profile.dev]
    split-debuginfo = "unpacked"
    
    • 1
    • 2

    稳定api

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

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    以下方法已稳定.

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    1.52.0稳定版

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

    已稳定以下方法.

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    以前稳定API现在是.

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.53.0稳定版

    数组的IntoIterator

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

    for i in [1, 2, 3] {
        ..
    }
    
    • 1
    • 2
    • 3

    以前,只能用&[1,2,3][1,2,3].iter()引用来实现.
    同样,你现在可把数组传递给需要T:IntoIterator的方法:

    let set = BTreeSet::from_iter([1, 2, 3]);
    for (a, b) in some_iterator.chain([1]).zip([1, 2, 3]) {
        ..
    }
    
    • 1
    • 2
    • 3
    • 4

    或模式

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

    match result {
         Ok(Some(1 | 2)) => { .. }
         Err(MyError { kind: FileNotFound | PermissionDenied, .. }) => { .. }
         _ => { .. }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Unicode标识

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

    const BL HAJ: &str = "  ";
    struct{
        名字: String,
    }
    let α = 1;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Cargo中的HEAD分支名支持

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

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

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    来自上海的联合办公企业堂堂加集团申请纳斯达克IPO上市
    ROS常用模板
    5G创新突破 | 紫光展锐5G芯片全球首发R17 NR广播端到端业务演示
    并发基础(三):线程
    Python自学笔记8:实操案例五(循环输出26个字母对应的ASCII码值,模拟用户登录,猜数游戏,计算100-999之间的水仙花数)
    数据分析实战-Python实现博客评论数据的情感分析
    IJ中配置TortoiseSVN插件:
    前端进击笔记第九节 HTTP 协议和前端开发有什么关系?
    前端秘法基础式终章----欢迎来到JS的世界
    revit二次开发——过滤器
  • 原文地址:https://blog.csdn.net/fqbqrr/article/details/134487434