error[E0658]: using `_` for array lengths is unstable

error[E0658]: const arguments cannot yet be inferred with `_`

ジェネリクス定数を '_' で推論しようとした。

原因

これは の Rust の制限である。

ナイトリー版が必要になるが、不安定な機能 generic_arg_infer で問題を改善できる。

パターン

パターン A

配列の要素数の省略

配列の要素が省略されているパターン。

サンプル


fn main() {
    let arr = [1, 2, 3] as [u32; _];
    assert_eq!(arr, [1u32, 2, 3]);
}

error[E0658]: using `_` for array lengths is unstable
 --> src\main.rs:2:34
  |
2 |     let arr = [1, 2, 3] as [u32; _];
  |                                  ^
  |
  = note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information

パターン B

型引数の省略

関数などの型引数が省略されているパターン。

サンプル


fn main() {
    let vec = Vector([1.0, 2.0, 2.0]);
	let abs = vec_abs::<_>(&vec);
    assert_eq!(abs, 3.0);
}

fn vec_abs<const N: usize>(vec: &Vector<N>) -> f32 {
    vec.0.iter().map(|x| x * x).sum::<f32>().sqrt()
}

struct Vector<const N: usize>(pub [f32; N]);

error[E0658]: const arguments cannot yet be inferred with `_`
 --> src\main.rs:3:25
  |
3 |     let abs = vec_abs::<_>(&vec);
  |                         ^
  |
  = note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information