We already had str::split and str::splitn, which result in an iterator. But when parsing something simple, you often want to split something exactly once. For example, to parse a string containing `key=value`.
2/10
Another one I'm excited about is one of the first features I worked on: std::fmt::Arguments::as_str()
fmt::Arguments is returned by format_args!(), and appears in macros that support formatting.
as_str() allows handling the literal case without special-casing your macro:
3/10
Next: #[deny(unsafe_op_in_unsafe_fn)]
This forces you to write `unsafe` to call unsafe functions, even when inside an unsafe fn. Most unsafe functions have a mostly (or fully) safe implementation, so separating 'unsafe to call' from 'unsafe implementation' is often useful.
4/10
The casting operator (`as`) already allowed converting from a reference to an array to a pointer to the first element (from &[T; N] to *const T). Starting in Rust 1.52, this also works for the mutable case (from &mut [T; N] to *mut T):
5/10
Another important library feature: slice::partition_point.
This performs a binary search through a slice, given a (boolean) predicate.
With `<= x` or `< x` as the predicate, this is equivalent to the `upper_bound` and `lower_bound` algorithms of the C++ standard library.
6/10
If you like reference-counted errors, Rust 1.52 now implements the Error trait for all Arc<T> for which T itself implements Error:
7/10
As in most recent Rust releases, more functions are turned into `const fn`s to allow use in compile-time evaluated expressions.
This time it's the ascii upper-/lowercase functions, char::len_utf8 and len_utf16, and all the integer division/remainder functions:
8/10
The last new feature I want to highlight is a Rustdoc feature: Task lists.
With the same syntax that's supported by some other markdown variants (e.g. GitHub's), you can now add checked and unchecked checkboxes in your documentation:
9/10
And with that, I'm ending this thread. :)
There's a few more changes and additions, which you can find in the full release notes: github.com/rust-lang/rustβ¦
10/10
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
ππ¦ 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.