• 2311rust,到46版本更新


    1.43.0稳定版

    项(item)片段

    中,可用片段把插值到特征,实现extern块的块体中.如:

    macro_rules! mac_trait {
        ($i:item) => {
            trait T { $i }
        }
    }
    mac_trait! {
        fn foo() {}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这生成:

    trait T {
        fn foo() {}
    }
    
    • 1
    • 2
    • 3

    围绕原语的推导类型

    改进了围绕原语,引用和二进制操作的推导类型.如下片段,

    let n: f32 = 0.0 + &0.0;
    
    • 1

    Rust1.42中,你会收到错误,说"嘿,不知道如何加f64&f64,而结果是f32.该算法现在正确地决定0.0&0.0都应该是f32.

    测试新的Cargo环境变量

    为了帮助整合测试,Cargo设置一些新的环境变量.
    假设正在处理叫"cli"的命令行项目.如果正在编写整合测试,想调用该cli二进制文件并查看它的作用.
    运行测试基准测试时,Cargo会设置可在测试中使用的叫CARGO_BIN_EXE_cli的环境变量:

    let exe = env!("CARGO_BIN_EXE_cli");
    
    • 1

    这使得调用cli更加容易,因为现在可直接调用.

    更改库

    现在,不必导入模块,可直接在浮点数和整数上使用关联常量.也即,现在不必用 std::u32;用 std::f32;,就可编写u32::MAXf32::NAN.

    有个可重新导出Rust原语类型的新的原语模块.编写宏并想确保类型不会被遮蔽时,很有用.

    此外,还稳定了6个新API:

    Once::is_completed
    f32::LOG10_2
    f32::LOG2_10
    f64::LOG10_2
    f64::LOG2_10
    iter::once_with
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Rust1.43.1

    1,修复了无法检测到的CPU功能
    2,修复破损的cargo package --list
    3,OpenSSL更新到1.1.1g

    1.44.0稳定版

    亮点是cargo中整合了cargo tree,并在no_std环境中支持async/await.

    1.45.0稳定版

    修复转换(cast)中的不健壮性
    rustc使用LLVM作为编译器后端.编写如下代码时:

    pub fn cast(x: f32) -> u8 {
        x as u8
    }
    
    • 1
    • 2
    • 3

    Rust1.44.0及更早版本中的Rust编译器生成如下的LLVM-IR:

    define i8 @_ZN10playground4cast17h1bdf307357423fcfE(float %x) unnamed_addr #0 {
    start:
      %0 = fptoui float %x to i8
      ret i8 %0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    fptoui实现了转换,它是"浮点到正整数"的缩写.
    但有个问题.文档中说:
    "fptoui"指令,把浮点数转换为最接近(圆整为零)的正整数值.如果该值不适合ty2,则结果有问题.
    即:如果转换大浮点数小整数,你会得到未定义行为.

    即,如,如下没有明确定义:

    fn cast(x: f32) -> u8 {
        x as u8
    }
    fn main() {
        let f = 300.0;
        let x = cast(f);
        println!("x: {}", x);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这就是所说的"健壮性"错误.
    不过,花了很久才解决该错误.原因是不清楚正确前进道路.
    最后,决定这样:
    1,as执行"饱和转换".
    2,如果想跳过检查,添加新的不安全转换.

    这与访问数组类似,如:
    1,检查array[i]以确保数组至少有i+1个元素.
    2,可用unsafe{array.get_unchecked(i)}跳过检查.
    什么是饱和转换?看看稍微修改下的示例:

    fn cast(x: f32) -> u8 {
        x as u8
    }
    fn main() {
        let too_big = 300.0;
        let too_small = -100.0;
        let nan = f32::NAN;
        println!("too_big_casted = {}", cast(too_big));
        println!("too_small_casted = {}", cast(too_small));
        println!("not_a_number_casted = {}", cast(nan));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这打印:

    too_big_casted = 255
    too_small_casted = 0
    not_a_number_casted = 0
    
    • 1
    • 2
    • 3

    即,太大的数字会变成最大可能值.太小的数字会产生最小的可能值(即零).NaN产生零.
    不安全方式转换的新API是:

    let x: f32 = 1.0;
    let y: u8 = unsafe { x.to_int_unchecked() };
    
    • 1
    • 2

    但如常,这只是用作最后的手段.

    稳定式,模式和语句中的类似函数的过程宏

    目标是不要求你编写不安全代码.
    像这样:

    gobject_gen! {
        class MyClass: GObject {
            foo: Cell<i32>,
            bar: RefCell<String>,
        }
        impl MyClass {
            virtual fn my_virtual_method(&self, x: i32) {
                ... 处理x ...
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    基本上只能在代码中的特定位置,调用gobject_gen!.
    Rust1.45.0在三个新地方,增加了调用过程宏的功能:

    //假定有叫`"mac"`的过程宏,
    mac!(); //项目位置,这是以前稳定的
    //但下三个是新的:
    fn main() {
      let expr = mac!(); //表达式位置
      match expr {
          mac!() => {} //模式位置
      }
      mac!(); //语句位置
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    下面是即将发布的火箭"helloworld"示例:

    #[macro_use] extern crate rocket;
    #[get("//")]
    fn hello(name: String, age: u8) -> String {
        format!("Hello, {} year old named {}!", age, name)
    }
    #[launch]
    fn rocket() -> rocket::Rocket {
        rocket::ignite().mount("/hello", routes![hello])
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    更改库

    Rust1.45.0中,以下API已稳定:

    Arc::as_ptr
    BTreeMap::remove_entry
    Rc::as_ptr
    rc::Weak::as_ptr
    rc::Weak::from_raw
    rc::Weak::into_raw
    str::strip_prefix
    str::strip_suffix
    sync::Weak::as_ptr
    sync::Weak::from_raw
    sync::Weak::into_raw
    char::UNICODE_VERSION
    Span::resolved_at
    Span::located_at
    Span::mixed_site
    unix::process::CommandExt::arg0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    此外,还可让char区间一起使用,以遍历代码点:

    for ch in 'a'..='z' {
        print!("{}", ch);
    }
    println!();
    //打印`"abcdefghijklmnopqrstuvwxyz"`
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.45.1稳定版

    修复使用引用的常量传播

    Rust1.45.0中,在确定是否传播给定常量时,rustc传播趟,没有正确处理引用,导致错误的行为.

    struct Foo {
        x: u32,
    }
    fn main() {
        let mut foo = Foo { x: 42 };
        let x = &mut foo.x;
        *x = 13;
        let y = foo;
        println!("{}", y.x); //`->42`;期望成果:`13`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.45.2稳定版

    #[track_caller]关于特征对象

    错误编译了带#[track_caller]注解方法的特征对象.#[track_caller]1.45上还不稳定.但是,标准库在某些特征上利用了它,以获得更好的错误消息. SliceIndex,IndexIndexMutTrait对象受此bug影响.

    元组模式绑定…到标识

    1.45.1中,向后移植了#74539的修复程序,但此修复程序是错误的,导致了其他不相关的破坏.因此,此版本修复还原该程序.

  • 相关阅读:
    02_diffusion_models_from_scratch_CN
    java项目-第139期ssm博客系统-ssm毕业设计_计算机毕业设计
    2023牛客OI赛前集训营-提高组(第二场) 集合
    吉客云对接打通金蝶云星空销售单查询接口与销售出库新增接口
    释放数据的潜力:用梯度上升法解锁主成分分析(PCA)的神奇
    一篇文章搞懂WPF动画的使用技巧
    JavaScript基础---JavaScript内置对象---10.20
    python考研志愿填报模拟系统vue
    选择HAL库还是标准库
    Flutter——软件安装与环境配置
  • 原文地址:https://blog.csdn.net/fqbqrr/article/details/134481387