我创建了一个结构,其中应存储文件或stdin上的迭代器,但编译器对我大吼大叫:)
我决定Lines
是我需要存储在我的struct中的结构,以便稍后使用它进行迭代,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
值的类型。你不能创建一个本地值(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,
}
如果你喜欢冒险,你可能会使用一些不安全的代码/ crate包装不安全的代码来存储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>>>,
}