Series: The Rust Annals

Vol. I Issue 49 nlopes.dev

Announcing Rust 1.48.0

Rust 1.48 makes rustdoc links aware of Rust items, so documentation authors no longer have to maintain relative URLs that can become inaccurate around re-exports. The release also adds rustdoc search aliases, stabilizes fallible conversion from vectors to fixed-length arrays, adds five standard-library APIs, and makes eight existing APIs usable in const contexts.

Rustdoc generates links to Rust items

The star of this release is rustdoc. Markdown does not understand Rust items or rustdoc-generated URLs, so links between items previously had to be written as relative paths:

pub mod foo {
    /// Some docs for `Foo`
    ///
    /// You may want to use `Foo` with [`Bar`].
    ///
    /// [`Bar`]: ../bar/struct.Bar.html
    pub struct Foo;
}

pub mod bar {
    /// Some docs for `Bar`
    ///
    /// You may want to use `Bar` with [`Foo`].
    ///
    /// [`Foo`]: ../foo/struct.Foo.html
    pub struct Bar;
}

Those paths are tedious and error-prone, and they cannot remain accurate in every view when an item is also re-exported. Rustdoc can now resolve links to types and generate their URLs:

pub mod foo {
    /// Some docs for `Foo`
    ///
    /// You may want to use `Foo` with [`Bar`](crate::bar::Bar).
    pub struct Foo;
}

pub mod bar {
    /// Some docs for `Bar`
    ///
    /// You may want to use `Bar` with [`crate::foo::Foo`].
    pub struct Bar;
}

The first form displays Bar while linking to crate::bar::Bar. The second links to Foo and displays the complete crate::foo::Foo path. The rustdoc book documents the available forms in Linking to items by name; an Inside Rust post covers the feature’s history.

Search aliases make items discoverable under another name

Items can now use #[doc(alias = "<alias>")] to add aliases to rustdoc’s search:

#[doc(alias = "bar")]
struct Foo;

Searching for “bar” will include Foo even though “Foo” is absent from the query. One interesting use is an FFI wrapper crate in which each Rust function is aliased to the C function it wraps, allowing users of the underlying C library to search for the corresponding Rust functions.

Vectors can be converted to arrays of a required length

The most significant library change stabilizes [T; N]: TryFrom<Vec<T>>. It permits an attempted conversion from a vector to an array whose length is fixed by N:

use std::convert::TryInto;

let v1: Vec<u32> = vec![1, 2, 3];

// This will succeed; our vector has a length of three, we're trying to
// make an array of length three.
let a1: [u32; 3] = v1.try_into().expect("wrong length");

// But if we try to do it with a vector of length five...
let v2: Vec<u32> = vec![1, 2, 3, 4, 5];

// ... this will panic, since we have the wrong length.
let a2: [u32; 3] = v2.try_into().expect("wrong length");

The conversion succeeds when the lengths match; in this example, calling expect on the failed conversion panics when they do not. This is an example of an API enabled by the standard library’s use of const generics, discussed in the previous release.

Five APIs stabilize and eight become const

Reproduced from the Rust blog under its publication licence. Typeset in Literata.