为什么不能使用“?”编译nth()调用?关键词?

时间:2018-09-14 09:54:58

标签: error-handling rust

我正试图了解Rust的?关键字。

为此,我编写了以下示例

use std::env;
fn main() {
    let first = env::args().nth(1)?;
    let n: i32 = first.parse()?;
    println!("Hello, world!");
}

我很确定env::args.nth(1)返回一个Option,它实现了Try特质。尝试编译时,出现以下错误消息

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src\main.rs:3:17
|
3 |     let first = env::args().nth(1)?;
|                 ^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`

我在做什么错了?

0 个答案:

没有答案