gpt4 book ai didi

带问号运算符的使用rust 错误 "the trait bound is not satisfied"

转载 作者:行者123 更新时间:2023-11-29 08:10:50 25 4
gpt4 key购买 nike

<分区>

use std::io;
use std::fs::File;
use std::io::prelude::*;

fn main() {
let mut csv = File::open("Item.csv")?;
}

这是我的部分代码,我遇到了错误:

   Compiling eracsv v0.1.0 (file:///C:/Users/jwm/Project/eracsv)
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> src\main.rs:
|
| let mut csv = File::open("Item.csv")?;
| -----------------------
| |
| the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
| in this macro invocation
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`

我已经处理了 rustc 1.19 stable 和 1.22 nightly,并且都出现了同样的错误。

但是,这与 rust doc 完全相同,不是吗?明确提到 File::open() 函数返回结果。

我很好奇为什么?运算符会导致编译错误,而 unwrap() 不会。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com