rust - 如何在结构中存储标准输入上的迭代器?
问题描述
我创建了一个结构,其中应该存储文件或标准输入的迭代器,但编译器对我大喊大叫:)
我决定这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`
我怎样才能实现我的目标?
解决方案
对您的问题最通用的答案是您不/不能。锁定标准输入会返回一个引用该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
值和引用它的迭代器,这不是普遍安全的。
也可以看看:
- 有没有办法在构造函数中使用锁定的标准输入和输出来与您正在构建的结构一样长?
- 有什么方法可以返回对函数中创建的变量的引用?
- 为什么我不能在同一个结构中存储一个值和对该值的引用?
- 如何将 Chars 迭代器存储在与其迭代的 String 相同的结构中?
- 是否允许多态变量?
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>>>,
}
推荐阅读
- android - 自定义适配器和 ListView 崩溃
- java - URLConnection inputStream 返回“Busy”为什么?
- java - 从 SwingWorker 线程返回数组到主线程
- r - nlme 中的多重随机效应
- sql - 表“test.grouped”不存在
- python - 分隔符问题,pd.read_csv
- gitlab - 使用 gitlab.com 托管的 Gitlab,如何在 Prometheus 中获取诸如“提交次数”之类的指标?
- hyperledger-fabric - 我们可以自定义交易类型或在链码中添加一些代码格式来阻止以确定交易类型吗?
- reporting-services - Power BI:关于隐私问题的建议
- python - 从 seaborn 图中删除图例标题