add running result for ? operator using in main function

pull/1149/head
sunface 2 years ago
parent 3973ba6719
commit 373288155d

@ -340,6 +340,21 @@ fn main() {
}
```
运行后会报错:
```shell
$ cargo run
...
the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:4:48
|
3 | fn main() {
| --------- this function should return `Result` or `Option` to accept `?`
4 | let greeting_file = File::open("hello.txt")?;
| ^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
```
因为 `?` 要求 `Result<T, E>` 形式的返回值,而 `main` 函数的返回是 `()`,因此无法满足,那是不是就无解了呢?
实际上 Rust 还支持另外一种形式的 `main` 函数:

Loading…
Cancel
Save