Unlike the seek() function from the Seek trait, this one does not return the current position in the stream, which allows the BufReader to perform the seek without accessing the underlying reader if the new position lies within the buffer.
6. NonZero*::leading_zeros() and NonZero*::trailing_zeros()
These avoid the zero-check that the same functions on the regular integer types need to do, since on many architectures, the corresponding native instruction doesn't handle the zero case.
These are both `const fn`s.
7. std::array::from_ref and std::array::from_mut
These can safely convert a &T to &[T; 1] or &mut T to &mut [T; 1].
8. f32::is_subnormal and f64::is_subnormal
A way to easily check if a floating point number is subnormal.
9. DebugStruct::finish_non_exhaustive()
This allows you to mark the custom Debug representation of your type with fmt.debug_struct() to not be exhaustive. It adds two dots before the closing brace.
We've also updated the std types' Debug impls to consistently use this.
10. AtomicPtr::fetch_update and AtomicBool::fetch_update
We already had this one on the other atomic types, but they were missing on atomic pointers and booleans.
11. Peekable::peek_mut()
This allows you to mutate the peeked value which will be returned next by the Iterator.
ππ¦ Just an hour ago, #rustlang 1.66.0 was released!
As usual, here's a thread with some of the highlights. π§΅
1/12
Rust 1.66 comes with std::hint::black_box(), a function that does nothing. However, the compiler tries its very best to pretend it doesn't know what it does.
It is useful in benchmarks, to prevent the compiler from optimizing your entire benchmark away.
2/12
The Option type got a new method: Option::unzip(). It's basically the opposite of Option::zip(): it splits an Option of a pair into a pair of Options.
Rust now has a new async-related trait: IntoFuture.
The .await syntax be used on anything that implements IntoFuture. (Similar to how, with a for loop, you can iterate over anything that implements IntoIterator.)
This allows types to provide easier async interfaces.
2/15
Today's Rust release also comes with two more async-related tools:
The std::future::poll_fn function allows you to easily create a future from a closure (like iter::from_fn for iterators).
The std::task::ready!() macro extracts a Poll::Ready, or returns early on Pending.
Cargo now has 'cargo add' built-in: a (sub)command to add a crate to your Cargo.toml. It automatically looks up the latest version, and shows you the available features of the crate.
See `cargo add --help` for more details.
2/9
On Linux and several BSDs, std::sync's Mutex, RwLock, and Condvar now no longer do any allocations. They used to be (heap-allocated) wrappers around pthread lock types, but have been replaced by a minimal, more efficient, futex-based implementations.
π¦ As of Rust 1.62 (going into beta this week), std::sync::Mutex, RwLock, and Condvar no longer do any allocations on Linux. π
Benchmarking locks is extremely tricky, as their performance depends heavily on the exact use case, but there are very noticable differences:
std's Mutex basically used to contain a Pin<Box<pthread_mutex_t>>, where the pinned Box was only necessary because pthread_mutex_t is not guaranteed movable. The new Mutex no longer uses pthread, and instead directly uses the futex syscall, making it smaller and more efficient.
Also, the new RwLock on Linux prefers writers, which prevents writer starvation. pthread_rwlock_t prefers readers by default, to allow recursive read locking. Rust's RwLock does not make recursion guarantees, and on several platforms (including Windows) already preferred writers.