问题描述
阅读 std::io::BufReader
文档后,我我不确定如何在函数之间最好地传递BufReader
.允许进行多种排列,但是哪一个是最好的?
After reading the std::io::BufReader
docs, I'm not sure how to best pass a BufReader
between functions. Multiple permutations are allowed, but which is best?
我有一个带文件的功能:
I have a function that takes a file:
use std::{fs::File, io::BufReader};
fn write_some_data(f: &mut std::fs::File) {
let mut reader = BufReader::new(f);
write_some_other_data(&mut reader);
}
这可以使之起作用,但是当将阅读器传递给其他功能时,应该使用哪种引用访问权限排列?
While this can be made to work, which permutation of reference access should be used when passing the reader around to other functions?
-
&mut BufReader<&mut File>
-
BufReader<&mut File>
-
&mut BufReader<File>
-
BufReader<File>
&mut BufReader<&mut File>
BufReader<&mut File>
&mut BufReader<File>
BufReader<File>
由于我不需要每个函数都拥有数据,因此我认为最好以&mut BufReader<&mut File>
的形式传递,但是文档中的示例使用了<File>
.
Since there is no need for each function to own the data I was thinking it would be best to pass as &mut BufReader<&mut File>
, but the example in the docs uses <File>
.
在这里使用一条好的经验法则是什么?
Whats a good rule of thumb to use here?
尽管此示例使用BufReader
,但我想同样的答案也适用于BufWriter
.
While this example uses BufReader
, I assume the same answer would apply to BufWriter
too.
推荐答案
最惯用的方式可能是根本不引用std::io::BufReader
.您实际上要引用特征Read
和/或BufRead
The most idiomatic way is probably not to reference std::io::BufReader
at all. You actually want to refer to the traits Read
and/or BufRead
use std::io:BufRead;
// Could also take by move if needed
fn read_data<R: BufRead>(r: &mut R);
该函数通常并不真正在乎读取器是否是std::io::BufReader
类型,只是它具有相同的功能.
The function usually doesn't really care whether a reader is specifically the type std::io::BufReader
, merely that it has the same functionality.
这也使您可以完全自由地选择BufReader<File>
,BufReader<&mut File>
或所需的其他任何专业. (它甚至不必是一个文件,可以帮助进行测试!)
This also gives you complete freedom to choose between BufReader<File>
, BufReader<&mut File>
or whichever other specialization you need. (It doesn't even have to be a file, which can help for testing!)
关于是否使用&mut
而不是移动,通常在Rust中,标准是仅请求您需要的内容.如果您(和您调用的函数)仅需要一个不变的引用(&T
),请使用该引用,如果您需要可变性,请使用&mut T
.
As for whether to use &mut
versus a move, generally in Rust it's standard to only request what you need. If you (and the functions you call) only require an immutable reference (&T
), use that, if you require mutability, use &mut T
.
Move稍微灵活一点,因为虽然可以根据是否需要使用按值获取值的函数来简单地使用它,但是它也经常被用来断言该函数会消耗"某些数据.道路.
Move is a bit more flexible, because while it can be used simply based on whether you need to use a function that takes something by value, it's also frequently used to assert that the function will "use up" the data in some way.
这就是为什么BufReader
通常采用File
而不是引用的原因,以及为什么大多数高级解析此文件" IO功能倾向于按值移动.通常情况下,您不会使用一个适配器消耗File
或阅读器的一部分,而使用另一个适配器消耗其余的.
This is why BufReader
usually takes a File
and not a reference, and why most high-level "parse this file" IO functions tend to move by value. It's generally not the case that you consume part of a File
or reader with one adapter, and the rest with another.
实际上,这在概念上是如此强大,以至于没有给出参考,更常见的是将File
移到更高级别的阅读器中,并在需要时调用into_inner
之类的函数来检索文件.切换适配器.
In fact, this is conceptually so strong that rather than giving a reference, it's much more common to just move the File
into a higher-level reader and call a function like into_inner
to retrieve the file whenever you need to switch adapters.
这篇关于在函数之间传递BufReader/BufWriter的惯用方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!