A smaller but very useful new feature is std::array::from_fn.
Use it to create an array based on a closure, which is called for every index of the array.
Not only the element type, but also the number of elements can be automatically inferred from context.
5/16
VecDeque<u8> now implements both io::Read and io::Write, such that it can be used by generic I/O functions like io::copy and write!().
Write appends to the end (just like Write for Vec<u8>), while Read reads and removes from the start, like a circular buffer.
6/16
Just like Vec and String and some other collections already had, BinaryHeap, OsString, and PathBuf now also have a `try_reserve` method for fallible allocation.
Rather than panicking when out of memory, this method returns an Result::Err that you can handle manually.
7/16
The ToOwned trait now has a new (provided) method: clone_into().
In some cases, it allows for more efficient operation than the to_owned() method, because it can re-use an already existing allocation of the object you're cloning into.
8/16
The old borrow checker is now completely phased out, leaving only the "non-lexical lifetimes" (NLL) borrow checker. This shouldn't change anything for you, other than some differences (hopefully improvements) in how lifetime errors are reported.
If a function uses both `<T: Trait>` and `impl Trait` syntax for generics in its signature, callers are now allowed to explicitly specify the generic arguments of the first form, even if the function also uses `impl Trait` syntax.
10/16
Today's release also includes a small bug fix in the language. It's technically a breaking change, but unlikely to affect any real world code.
Writing [expr; 0] (to create an array of zero elements) would not drop `expr`, but instead forget (leak) it. That has been fixed.
11/16
Another (very minor) breaking change to the language also involves Drop. Enums that implement Drop can no longer be casted (with the `as` operator) to an integer.
(Casting enums to integers was already restricted to enums that don't have any fields, aka C-style enums.)
12/16
The documentation now includes some clarification on what `unsafe` means in edge cases like using File to write to /proc/self/mem, or using Command to spawn a debugger. These fall outside of the safety guarantees of Rust, and File::open will remain a safe function.
13/16
Initial support for the Nintendo 3DS has been added to the Rust standard library. It's not an officially supported platform, so no guarantees, but this should make it easier to develop Rust software for this platform.
In addition, the Rust compiler can now target Apple's watchOS. This is also an unsupported platform, so no guarantees, but it should now be possible to compile (no_std) Rust code for watchOS. βοΈ
(Support for `std` ships in the next release, 1.64.)
ππ¦ 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.
Newly stabilized in the standard library is File::options(). It's identical to OpenOptions::new(), but you don't have to import the OpenOptions type separately from the File type.