error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types

error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants

error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables

型推論の未対応箇所で '_' を使おうとした。

原因

型推論の未対応箇所』では型推論の未対応箇所を紹介している。

それらの箇所で '_' を使ってしまうとエラーになる。

パターン

パターン A

関数の返値型での型推論

関数の返値型において、'_' で型推論しようとした。

サンプル

以下では、'_' で関数 func の返値型を型推論している。


fn func() -> _ {
    true
}

error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
 --> src\lib.rs:1:14
  |
1 | fn func() -> _ {
  |              ^
  |              |
  |              not allowed in type signatures
  |              help: replace with the correct return type: `bool`

解決策

以下のように型を明示するとよい。


fn func() -> bool {
    true
}

パターン B1

conststatic での型推論 (通常型)

const または static のアイテムにおいて、'_' で通常の型を型推論しようとした。

サンプル

以下では、'_' で型 Option<T> の型パラメタ T を型推論している。


pub static OPT: Option<_> = Some(false);

error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
 --> src\lib.rs:1:24
  |
1 | pub static OPT: Option<_> = Some(false);
  |                        ^ not allowed in type signatures
  |
help: replace this with a fully-specified type
  |
1 - pub static OPT: Option<_> = Some(false);
1 + pub static OPT: Option<bool> = Some(false);
  |

解決策

以下のように型を明示するとよい。


pub static OPT: Option<bool> = Some(false);

パターン B2

conststatic での型推論 (要素数)

const または static のアイテムにおいて、'_' で配列の要素数を型推論しようとした。

サンプル

以下では、'_' で型 [bool; N] の型パラメタ N を型推論している。


pub const ARR: [bool; _] = [false, false, true];

error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
 --> src\lib.rs:1:23
  |
1 | pub const ARR: [bool; _] = [false, false, true];
  |                       ^ not allowed in type signatures
  |
help: replace this with a fully-specified type
  |
1 - pub const ARR: [bool; _] = [false, false, true];
1 + pub const ARR: [bool; 3] = [false, false, true];
  |

解決策

以下のように要素数を明示するとよい。


pub static ARR: [bool; 3] = [false, false, true];