LoginSignup
13
2

More than 1 year has passed since last update.

2021年でconst文脈でできるようになったこと

Last updated at Posted at 2021-12-20

この記事はRust Advent Calendar 2021 2の20日目の記事です。


2021年(1.50.0 ~ 1.57.0)にconst文脈でできるようになったことを書いていきます。またconstの話です。
英語が得意ではないのでミス、抜け漏れなどがあれば指摘していただけるとありがたいです。
内容はrust-lang/rustリポジトリのREADME.mdを参考にしています。


1.50.0 (2021-02-11)

You n now use const values for x in [x; N] array expressions. This has been technically possible since 1.38.0, as it was unintentionally stabilized.

[x; N]x部分にconstな値を入れられるようになったというものです。

This has been technically possible since 1.38.0, as it was unintentionally stabilized.
意訳: これは1.38からできたけど意図せず安定していた
とあるのでおそらく既にコンパイルが通っていたが規格として決めていなかったものを決めたものだと思います。

1.51.0 (2021-03-25)

You can now parameterize items such as functions, traits, and structs by constant values in addition to by types and lifetimes. Also known as "const generics" E.g. you can now write the following. Note: Only values of primitive integers, bool, or char types are currently permitted.

Genericsにconstな値を渡せるようになる、つまりconst genericsです。
これについては去年のアドカレの記事があるのでそちらを読んでいただければどんな機能かわかると思います。

rustc no longer promotes division, modulo and indexing operations to const that could fail.

0除算や配列の範囲外へインデックスアクセスするのをエラーにします。

1.52.0 (2021-05-06)

All integer division and remainder operations are now const.

整数型の除算と余りの計算をする関数(i*::checked_divなど)をconstにする。

関数一覧
  • i*::checked_div
  • i*::checked_div_euclid
  • i*::checked_rem
  • i*::checked_rem_euclid
  • i*::div_euclid
  • i*::overflowing_div
  • i*::overflowing_div_euclid
  • i*::overflowing_rem
  • i*::overflowing_rem_euclid
  • i*::rem_euclid
  • i*::wrapping_div
  • i*::wrapping_div_euclid
  • i*::wrapping_rem
  • i*::wrapping_rem_euclid
  • u*::checked_div
  • u*::checked_div_euclid
  • u*::checked_rem
  • u*::checked_rem_euclid
  • u*::div_euclid
  • u*::overflowing_div
  • u*::overflowing_div_euclid
  • u*::overflowing_rem
  • u*::overflowing_rem_euclid
  • u*::rem_euclid
  • u*::wrapping_div
  • u*::wrapping_div_euclid
  • u*::wrapping_rem
  • u*::wrapping_rem_euclid

1.53.0 (2021-06-17)

Add the BITS associated constant to all numeric types.

数値型(i32f64)にBITSという定数が追加されました。

1.54.0 (2021-07-29)

You can now cast between unsized slice types (and types which contain unsized slices) in const fn.

サイズが不明なsliceからサイズが不明なsliceに変換可能になります
↓こういうことができるようになるらしいですが何に使えるのかよくわかってないです。

const fn test() {
    let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
}

1.55.0 (2021-09-09)

The following previously stable functions are now const.

以下の関数がconst fnになりました

  • str::from_utf8_unchecked
fn main() {
    const SPARKLE_HEART_ARRAY: [u8; 4] = [240, 159, 146, 150];
    const SPARKLE_HEART: &str = unsafe { std::str::from_utf8_unchecked(&SPARKLE_HEART_ARRAY) };

    assert_eq!("💖", SPARKLE_HEART);
}

Rust Playground

1.56.0 (2021-10-21)

Union field access is permitted in const fn.

const fnの中でUnionのフィールドにアクセスできるようになりました。

These APIs are now usable in const contexts:

下記の関数がconst文脈で使用可能になりました。

  • std::mem::transmute
  • [T]::first
  • [T]::split_first
  • [T]::last
  • [T]::split_last

1.57.0 (2021-12-02)

Allow panicking in constant evaluation.

const文脈の中でpanic!マクロが使用できるようになりました。

const fn div(a: i32, b: i32) -> i32 {
    if b == 0 { panic!("b is 0"); }
    a / b
}

fn main() {
    const V1: i32 = div(10, 5);
    const V2: i32 = div(1, 0);  // panic
}

panic!に渡した文字列もコンパイルエラーのメッセージに表示されます。
Rust Playground

These APIs are now usable in const contexts:

下記の関数がconst文脈で使用可能になりました。

  • hint::unreachable_unchecked

まとめ

多くの関数や機能がconst文脈で使えるようになってきました。
この調子でどんどん実行時にできることとconst文脈でできることの差を縮めて欲しいです。
impl const Trait for Tyとか早く使えるようになると嬉しいです。

13
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
2