The Rust team recently released a new version of Rust 1.66.0. The main updates in the 1.66.0 stable version are as follows:
Explicit Discrimination for Enumerations with Fields
Enums with integer representations can now use explicit discrimination even if they have fields.
#[repr(u8)]
enum Foo {
A(u8),
B(i8),
C(bool) = 42,
}
Previously, you could use explicit discrimination on represented enums, but only if none of their variants had fields. Explicit discrimination is useful when passing values across language boundaries, since the representation of the enum needs to match in both languages.
core::hint::black_box
When benchmarking or inspecting the machine code produced by a compiler, it is often useful to prevent optimizations from happening in certain places.In the example below, the function push_cap
executed in a loop Vec::push
4 times.
fn push_cap(v: &mut Vec<i32>) {
for i in 0..4 {
v.push(i);
}
}
pub fn bench_push() -> Duration {
let mut v = Vec::with_capacity(4);
let now = Instant::now();
push_cap(&mut v);
now.elapsed()
}
If you check the optimization output of the compiler on x86_64, you’ll see that it looks rather short.
example::bench_push:
sub rsp, 24
call qword ptr [rip + std::time::Instant::now@GOTPCREL]
lea rdi, [rsp + 8]
mov qword ptr [rsp + 8], rax
mov dword ptr [rsp + 16], edx
call qword ptr [rip + std::time::Instant::elapsed@GOTPCREL]
add rsp, 24
ret
In fact, we want to benchmark the entire function push_cap
It has been optimized away!
We can use the new stable black_box
function to solve this problem. Functionally, black_box
It takes the value you pass it and passes it back directly.However, internally, the compiler will black_box
Think of it as a function that can do anything to its input and return any value.
This is useful for disabling optimizations like the one we saw above.
use std::hint::black_box;
fn push_cap(v: &mut Vec<i32>) {
for i in 0..4 {
v.push(i);
black_box(v.as_ptr());
}
}
cargo remove
In Rust 1.62.0 we introduced cargo add
, a command-line tool for adding dependencies to your project.now you can use cargo remove
to remove dependencies.
stable API
[proc_macro::Span::source_text](<https://doc.rust-lang.org/stable/proc_macro/struct.Span.html#method.source_text>)
[u*::{checked_add_signed, overflowing_add_signed, saturating_add_signed, wrapping_add_signed}](<https://doc.rust-lang.org/stable/std/primitive.u8.html#method.checked_add_signed>)
[i*::{checked_add_unsigned, overflowing_add_unsigned, saturating_add_unsigned, wrapping_add_unsigned}](<https://doc.rust-lang.org/stable/std/primitive.i8.html#method.checked_add_unsigned>)
[i*::{checked_sub_unsigned, overflowing_sub_unsigned, saturating_sub_unsigned, wrapping_sub_unsigned}](<https://doc.rust-lang.org/stable/std/primitive.i8.html#method.checked_sub_unsigned>)
[BTreeSet::{first, last, pop_first, pop_last}](<https://doc.rust-lang.org/stable/std/collections/struct.BTreeSet.html#method.first>)
[BTreeMap::{first_key_value, last_key_value, first_entry, last_entry, pop_first, pop_last}](<https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html#method.first_key_value>)
[impl TryFrom<Vec<T>> for Box<[T; N]>](<https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#impl-TryFrom%3CVec%3CT%2C%20Global%3E%3E-for-Box%3C%5BT%3B%20N%5D%2C%20Global%3E>)
[core::hint::black_box](<https://doc.rust-lang.org/stable/std/hint/fn.black_box.html>)
[Duration::try_from_secs_{f32,f64}](<https://doc.rust-lang.org/stable/std/time/struct.Duration.html#method.try_from_secs_f32>)
[Option::unzip](<https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.unzip>)
[std::os::fd](<https://doc.rust-lang.org/stable/std/os/fd/index.html>)
other changes
There are other changes in Rust 1.66, including
- You can now use in the pattern
..=X
scope. - The Linux version now optimizes the rustc frontend and LLVM backend with LTO and BOLT respectively, improving runtime performance and memory usage.
More details can be found at: https://blog.rust-lang.org/2022/12/15/Rust-1.66.0.html
#Rust #released #News Fast Delivery