Rust 1.45.0:稳定功能性程序宏,修复转换缺陷

Rust团队很高兴地宣布发布新版本1.45.0。Rust是一种编程语言,它使任何人都可以创建可靠而高效的软件。



如果使用tools安装了Rust的早期版本rustup,那么要升级到1.45.0版,您只需运行以下命令:



rustup update stable


如果你不已经拥有了它rustup,你可以安装它从我们的网站上相应的页面,也期待它在GitHub上



稳定版1.45.0中包含什么



该版本包含两个重大更改:修复了整数和浮点数之间转换中的长期缺陷,以及稳定了至少一个Web框架在稳定的Rust上运行所必需的功能。



纠正转换中的缺陷



最初的10184期于2013年10月开放,历时一年半才发布Rust 1.0。由于它rustc使用LLVM作为其后端编译器,因此在编写如下代码时:



pub fn cast(x: f32) -> u8 {
    x as u8
}


1.44.0及更早版本中的Rust编译器生成以下LLVM-IR:



define i8 @_ZN10playground4cast17h1bdf307357423fcfE(float %x) unnamed_addr #0 {
start:
  %0 = fptoui float %x to i8
  ret i8 %0
}


fptoui "floating point to unsigned integer".



, :



‘fptoui’ ( ) . ty2, .

The ‘fptoui’ instruction converts its floating-point operand into the nearest (rounding towards zero) unsigned integer value. If the value cannot fit in ty2, the result is a poison value.


, , . , : , .



, , :



fn cast(x: f32) -> u8 {
    x as u8
}

fn main() {
    let f = 300.0;

    let x = cast(f);

    println!("x: {}", x);
}


Rust 1.44.0 "x: 0", .. , . «» ( unsafe ) — , . I-unsound, .



. , , .



:



  • as " " (saturating cast),
  • unsafe , .


, :



  • array[i] , , array i + 1 ,
  • 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));
}


:



too_big_casted = 255
too_small_casted = 0
not_a_number_casted = 0


. ( ). NaN .



API :



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


, , . , , , .



,



Rust 1.30.0 « ». , gnome-class:



Gnome- — Rust. Rust- -, , GObject, , , GObject. , .

:



gobject_gen! {
    class MyClass: GObject {
        foo: Cell<i32>,
        bar: RefCell<String>,
    }

    impl MyClass {
        virtual fn my_virtual_method(&self, x: i32) {
            ... do something with x ...
        }
    }
}


" " — , , gobject_gen! .



Rust 1.45.0 :



// ,      "mac"

mac!(); //  , ,    

//    3 :
fn main() {
  let expr = mac!(); //  

  match expr {
      mac!() => {} //  
  }

  mac!(); //  
}


, , : Rocket. - Rocket, 2016 , , Rust. ", " :



#[macro_use] extern crate rocket;

#[get("/<name>/<age>")]
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])
}


Rocket . , , Rocket proc_macro_hygiene . , , ! Rocket. !



Rocket , , :)





Rust 1.45.0 :





char :



for ch in 'a'..='z' {
    print!("{}", ch);
}
println!();
//  "abcdefghijklmnopqrstuvwxyz"


.





, Cargo Clippy .



1.45.0



, Rust 1.45.0. , !





Rust - . , .



nlinker, funkill, Hirrolot blandger.




All Articles