6月14日、RustチームはRust 1.79.0をリリースした。
既にrustup
を使用してRustの以前のバージョンをインストールしている場合、次のコマンドで1.79.0をインストールできる。
$ rustup update stable
Rust、Cargo、Clippyに関する全て変更点は以下のリンクから確認できる。
以下に、Rustに関する変更点をまとめて紹介する。
Rust 1.79.0の新機能
インラインconst
式
const { ... }
ブロックが式位置で安定化され、追加の宣言を必要とせずにconst
コンテキストに明示的に入ることができるようになった。これにより、以下のようなパターンが可能になる。
const EMPTY: Option<Vec<u8>> = None;
let foo = [EMPTY; 100];
このようなコードは以下のように書き換えられる。
let foo = [const { None }; 100];
さらに、ジェネリックコンテキストでも、この新しい構文によりコードが簡潔になる。
fn create_none_array<T, const N: usize>() -> [Option<T>; N] {
[const { None::<T> }; N]
}
型バウンダリの構文を安定化
Rust 1.79は型バウンダリ構文を安定化し、追加のジェネリック型を提供することなく関連型を制約することができるようになった。この機能により、以下のような制約を指定できる。
where
句 -where T: Trait<Assoc: Bound>
はwhere T: Trait, <T as Trait>::Assoc: Bound
と同等。- スーパータイプ - 新しい構文を使った制約は、
where
句とは異なり、トレイトが使用される際に暗黙的に適用される。例:trait CopyIterator: Iterator<Item: Copy> {}
。 - 関連型のバウンダリ - トレイトの関連型に関連するネストされたリジッドプロジェクションを制約できる。例:
trait Trait { type Assoc: Trait2<Assoc2: Copy>; }
。 - 不透明型のバウンダリ - 不透明型に関連する関連型を制約することができる。例:
impl Iterator<Item: Copy>
は、不透明型のアイテムがCopy
であることを定義できる。
一時的なライフタイム延長の拡張
即座に参照される一時変数は、match
およびif
構文内で自動的にライフタイムが延長されるようになった。これは、ブロック構文内の一時変数のライフタイム延長と同様の動作をする。以下はその例である。
let a = if true {
..;
&temp() // 以前はエラーだったが、今はライフタイムが延長される
} else {
..;
&temp() // 以前はエラーだったが、今はライフタイムが延長される
};
let a = match () {
_ => {
..;
&temp() // 以前はエラーだったが、今はライフタイムが延長される
}
};
この動作は後方互換性があり、これらのプログラムは以前はコンパイルに失敗していた。
標準ライブラリビルドでのフレームポインタ有効化
Rustプロジェクトによって配布される標準ライブラリは、-Cforce-frame-pointers=yes
でコンパイルされるようになり、下流のユーザーがプログラムをより簡単にプロファイルできるようになった。標準ライブラリには引き続き行レベルのデバッグ情報(例:DWARF)が含まれているが、これはデフォルトでストリップされることに注意していただきたい。
詳細はAnnouncing Rust 1.79.0を参照していただきたい。