Series: The Rust Annals
Vol. I Issue 19 nlopes.dev
Announcing Rust 1.18
Restricted visibility and automatic field reordering combine a substantial language improvement with a consequential layout change.
Rust 1.18 publishes the first draft of the official book’s rewritten second edition, introduces restricted visibility for crate- and module-internal APIs, and starts automatically reordering fields where layout is unspecified to reduce padding. It also adds Windows subsystem selection, a CommonMark preview for rustdoc, seven standard-library APIs, and broader Cargo target, platform, and version-control support.
The rewritten Rust book reaches stable documentation
One of this release’s largest changes is the arrival of the first draft of the second edition of The Rust Programming Language in Rust’s online documentation. Nineteen of its twenty chapters have drafts; the chapter 20 draft will arrive with Rust 1.19.
The new edition is a complete rewrite based on two years of experience teaching Rust. It provides new explanations of core concepts and new projects to build. More than one hundred people have contributed through the book’s open development on GitHub. Editorial work was still continuing, and a print edition was planned through No Starch Press.
Restricted visibility makes internal APIs easier to express
Items remain private by default, but pub can now carry a restriction. Crate-wide visibility can be written as:
pub(crate) bar;bar is public throughout its crate but not outside it, making crate-internal APIs easier to declare than was often practical with the existing module system.
A path restriction narrows visibility to a module hierarchy:
pub(in a::b::c) foo;Here, foo is usable within the hierarchy of a::b::c, but not elsewhere. The design is specified by RFC 1422 and covered by the visibility reference.
Unspecified layouts can now be reordered to reduce padding
Rust has always left the layout of tuples, enum variant fields, and structs without #[repr] unspecified. Rust 1.18 turns on automatic reordering for these layouts, allowing the compiler to reduce padding and produce smaller values.
On x86_64, this tuple struct occupied six bytes in earlier releases:
struct Suboptimal(u8, u16, u8);The u16 required one padding byte before it, and the whole struct required another at the end: 1 + 1 (padding) + 2 + 1 + 1 (padding) = 6 bytes. Reordering it is analogous to this four-byte layout:
struct Optimal(u8, u8, u16);That arrangement needs no padding: 1 + 1 + 2 = 4 bytes. In Rust 1.18, both Suboptimal and Optimal have a size of four bytes.
The optimization had previously been available on nightly, but was rolled back after unsafe code was found to depend on exact representation details. Known instances were fixed before it was enabled again. Code that breaks because of the change should be reported so it can be repaired.
Windows applications also gain #![windows_subsystem]:
#![windows_subsystem = "console"]
#![windows_subsystem = "windows"]The attribute controls the linker’s /SUBSYSTEM flag. Only "console" and "windows" are currently supported. Selecting "windows" for a graphical application prevents a console window from flashing when the application starts.
rustdoc previews CommonMark while rustc builds faster
rustdoc’s planned move from Hoedown to a CommonMark-compliant Markdown parser now has a transition mechanism. The new --enable-commonmark flag selects the new parser while the old parser remains the default. Both parsers were believed to produce identical results, but users were asked to try the flag and report cases where rendering differs before the eventual switch.
Compiling rustc itself is now 15%–20% faster after internal inefficiencies were cleaned up.
Standard-library and Cargo additions broaden routine workflows
Seven standard-library APIs become stable:
Child::try_waitis a non-blocking form ofChild::wait.HashMap::retainandHashSet::retainbring theretainAPI fromVec<T>to the two hash data structures.PeekMut::poppops the top element from aBinaryHeap<T>after it has been peeked at, without reordering the heap a second time.TcpStream::peek,UdpSocket::peek, andUdpSocket::peek_fromallow peeking at a stream or socket.
Cargo adds support for the Pijul version-control system; cargo new my-awesome-project --vcs=pijul creates a project configured for it. New --bins, --examples, --tests, and --benches flags supplement --all by building every program of the selected type. Cargo also now supports Haiku and Android.