小编Don*_*Lee的帖子

为什么 Option 类型的 try 块需要类型注释?

编译器:rustc 1.71.0-nightly (c609da59d 2023-04-18)

我每晚都尝试#![feature(try_blocks)]这样做:

#![feature(try_blocks)]
fn test_try(input: Option<i32>) {
    let output = try {
        input?
    };

    println!("{:?}", output);
}
Run Code Online (Sandbox Code Playgroud)

但编译器声称output需要类型注释。完整的错误消息在这里:

error[E0282]: type annotations needed
 --> src/main.rs:3:9
  |
3 |     let output = try {
  |         ^^^^^^
  |
help: consider giving `output` an explicit type
  |
3 |     let output: /* Type */ = try {
  |               ++++++++++++
Run Code Online (Sandbox Code Playgroud)

如果我尝试let output: Option<i32> = ...一切正常。

看起来output应该是,Option<i32>但编译器并没有推断出这一点。

仅仅是因为该功能不稳定还是我错过了什么?除 之外还有其他类型Option<i32>output …

rust

4
推荐指数
1
解决办法
131
查看次数

标签 统计

rust ×1