archive2018

Moving a small crate to the Rust 2018 edition

Rust 1.31 shipped the Rust 2018 edition. What an edition is, what is different in daily code, and how to move a crate with cargo fix.

3 min read#dev#rust

This weekend I moved my two small Rust tools, a log parser and a sensor reader for a Raspberry Pi, to the Rust 2018 edition. It arrived with Rust 1.31 on 6 December 2018.

An edition is a set of language changes that you turn on per crate, in Cargo.toml. Old code is not forced to change. A crate on edition 2015 and a crate on edition 2018 can depend on each other in one program, because the compiler supports both. New crates made with cargo new on Rust 1.31 start on 2018.

What changes in daily code

The borrow checker is smarter now (non-lexical lifetimes). A borrow ends where it is last used, not at the end of the block. This was rejected before and compiles now:

let mut v = vec![1, 2, 3];
let first = &v[0];
println!("{}", first);
v.push(4);   // OK in 2018: `first` is not used after this point

For now this is only on for crates that use edition 2018; the Rust team says it will come to edition 2015 in a later release. So you get it after moving the crate to 2018 (next section), without other code changes. For me it is the best part of the release.

The change you see most is module paths. extern crate foo; is not needed anymore, because dependencies in Cargo.toml are in scope. Macros from other crates can be imported with use, like use serde_derive::Deserialize;. Paths to your own crate start with crate::.

// 2015
extern crate serde_json;
use config::Settings;      // from the crate root

// 2018
use crate::config::Settings;
// serde_json is available without extern crate

Trait objects are written Box<dyn Trait> instead of Box<Trait>. The old form still compiles with a warning.

async, await, dyn and try are keywords in 2018. A variable named async must be renamed, or written as the raw identifier r#async.

The steps

Commit first. cargo fix changes files in place, and you want a clean git tree to see the diff.

rustup update stable
cargo fix --edition

This still compiles as 2015, but prepares the code for 2018. Then set the edition:

[package]
name = "sensor-reader"
version = "0.2.0"
edition = "2018"
cargo build && cargo test

Optionally, move the code to the new idioms, like removing extern crate:

cargo fix --edition-idioms

This step is less automatic. I read the whole diff.

cargo fix only fixes code that is compiled. Code behind feature flags or cfg for another platform stays as it was, so run it with --all-features and for each target you build. For the Pi build I also had to update the target with rustup target add armv7-unknown-linux-gnueabihf. Some macro-generated code may need manual fixes. I did not hit that.

After the move, the crate needs Rust 1.31 or newer. That is fine for my tools. For a published library it matters for users on older compilers, so a version bump and a changelog line are polite.

rustfmt and Clippy

With this release, rustfmt and Clippy are ready for stable use. I added both to CI:

rustup component add rustfmt clippy
cargo fmt -- --check
cargo clippy -- -D warnings

Clippy found a few things in the log parser that I would not have noticed.