简体   繁体   English

if let语句中的类型不匹配:找到期望结果

[英]Mismatched types in if let statement: found Option expected Result

I am trying to parse a file that has a pipe delimiter between each value and each line is a new record. 我试图解析一个文件,在每个值之间有一个管道分隔符,每一行都是一个新记录。 I am iterating over each line like this: 我正在迭代每一行,如下所示:

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

fn main() {
    let source_file = File::open("input.txt").unwrap();
    let reader = BufReader::new(source_file);
    for line in reader.lines() {
        if let Some(channel_line) = line {
            println!("yay");
        }
    }
}

Playground 操场

However, I get an error: 但是,我收到一个错误:

error[E0308]: mismatched types
  --> src/main.rs:9:20
  |
9 |             if let Some(channel_line) = line {
  |                    ^^^^^^^^^^^^^^^^^^ expected enum    `std::result::Result`, found enum `std::option::Option`
  |
  = note: expected type `std::result::Result<std::string::String, std::io::Error>`
  = note:    found type `std::option::Option<_>`

This error is confusing to me as the found type is what I expected namely Option<Result<String, Error>> as indicated by the docs so it would make sense to unwrap the Option before the result assuming I am not missing something. 这个错误让我感到困惑,因为找到的类型是我所期望的,即文档所指示的Option<Result<String, Error>>所以在假设我没有遗漏某些内容之前在结果之前解包Option有意义的。

You linked to the documentation of next , but you're not using next (not directly anyway), you're using a for loop. 你链接到next的文档,但是你没有使用next (不是直接使用),你正在使用for循环。 In a for loop the type of the iteration variable (ie the type of line in your code) is the Item type of the iterator, which in your case is Result<String> . 在for循环中,迭代变量的类型(即代码中的line的类型)是迭代器的Item类型,在您的情况下是Result<String>

The type of next is Option<Item> because you might call next on an iterator that already reached the end. next类型是Option<Item>因为您可能会在已经到达结尾的迭代器上调用next The body of a for loop won't ever execute after the end of the iteration, so there's no point in the iteration variable being an option. for循环的主体在迭代结束后不会执行,因此迭代变量中没有任何一个选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust 预期类型不匹配 (),找到结构`Enumerate - Rust mismatched types expected (), found struct `Enumerate 不匹配的类型错误:预期的 `char`,找到参考 - Mismatched types error: expected `char`, found reference 不匹配的类型需要 `i8`,发现 `()` - mismatched types expected `i8`, found `()` “不匹配的类型预期单元类型`()`找到枚举`选项<String> `&quot; 尝试使用 Tauri 读取文件内容时出错 - "mismatched types expected unit type `()` found enum `Option<String>`" Error when trying to read the content of a file with Tauri “不匹配的类型:预期的 `i16`,发现 `Result`”在猜数字游戏中 - "mismatched types: expected `i16`, found `Result`" in a number guessing game Rust:错误,类型不匹配。 找到 std::result::Result - Rust: Error, mismatched types. Found std::result::Result 类型不匹配:在分配字符串时预期&str找到了字符串 - Mismatched types: expected &str found String when assigning string 返回的结构类型不匹配(预期 <K, V> ,找到&lt;&K,&V&gt;) - Mismatched types for returned struct (expected <K, V>, found <&K, &V>) 不匹配的类型预期特征 object `dyn Trait` 找到结构 `Struct` - mismatched types expected trait object `dyn Trait` found struct `Struct` 错误 [E0308]: 不匹配的类型 — 预期为 `&amp;str`,找到结构体 `std::string::String` - error[E0308]: mismatched types — expected `&str`, found struct `std::string::String`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM