首页 > 解决方案 > 如何在结构中存储标准输入上的迭代器?

问题描述

我创建了一个结构,其中应该存储文件或标准输入的迭代器,但编译器对我大喊大叫:)

我决定这Lines是我需要存储在我的结构中以便以后迭代使用它的结构,并且Box允许存储大小未知的变量,所以我这样定义我的结构:

pub struct A {
    pub input: Box<Lines<BufRead>>,
}

我想稍后做这样的事情:

let mut a = A {
    input: /* don't know what should be here yet */,
};
if something {
    a.input = Box::new(io::stdin().lock().lines());
} else {
    a.input = Box::new(BufReader::new(file).lines());
}

最后

for line in a.input {
    // ...
}

但是我从编译器得到一个错误

error[E0277]: the size for values of type `(dyn std::io::BufRead + 'static)` cannot be known at compilation time
  --> src/context.rs:11:5
   |
11 |     pub input: Box<Lines<BufRead>>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `(dyn std::io::BufRead + 'static)`
   = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-sized>
   = note: required by `std::io::Lines`

我怎样才能实现我的目标?

标签: rustiterator

解决方案


对您的问题最通用的答案是您不/不能。锁定标准输入会返回一个引用该Stdin值的类型。您不能创建一个局部值 ( stdin()),对其进行引用 ( .lock()),然后返回该引用。

如果您只想在函数内部执行此操作而不返回它,那么您可以创建一个特征对象:

use std::io::{self, prelude::*, BufReader};

fn example(file: Option<std::fs::File>) {
    let stdin;
    let mut stdin_lines;
    let mut file_lines;

    let input: &mut Iterator<Item = _> = match file {
        None => {
            stdin = io::stdin();
            stdin_lines = stdin.lock().lines();
            &mut stdin_lines
        }
        Some(file) => {
            file_lines = BufReader::new(file).lines();
            &mut file_lines
        }
    };

    for line in input {
        // ...
    }
}

或者创建一个新的泛型函数,您可以将任一类型的具体迭代器传递给:

use std::io::{self, prelude::*, BufReader};

fn example(file: Option<std::fs::File>) {
    match file {
        None => finally(io::stdin().lock().lines()),
        Some(file) => finally(BufReader::new(file).lines()),
    }
}

fn finally(input: impl Iterator<Item = io::Result<String>>) {
    for line in input {
        // ...
    }
}

您可以将特征对象或泛型类型放入结构中,即使您无法返回它:

struct A<'a> {
    input: &mut Iterator<Item = io::Result<String>>,
}
struct A<I> 
where
    I: Iterator<Item = io::Result<String>>,
{
    input: I,
}

如果您喜欢冒险,您也许可以使用一些不安全代码/包装不安全代码的 crates 来存储Stdin值和引用它的迭代器,这不是普遍安全的。

也可以看看:

input: Box<Lines<BufRead>>,

这是无效的,因为Lines它不是特征。你想要:

use std::io::{prelude::*, Lines};

pub struct A {
    pub input: Lines<Box<BufRead>>,
}

或者

use std::io;

pub struct A {
    pub input: Box<Iterator<Item = io::Result<String>>>,
}

推荐阅读