我知道 super
允许您访问“父”范围,但我从 std::io
中偶然发现了这段代码我不确定它是如何工作的。
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
where R: Read, W: Write
{
let mut buf = [0; super::DEFAULT_BUF_SIZE];
let mut written = 0;
loop {
let len = match reader.read(&mut buf) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
written += len as u64;
}
}
它如何访问 super::DEFAULT_BUF_SIZE
?我可以看到它是在 sys_common::io
中声明的,但我看不到这个模块如何成为 util
模块的父级。
最佳答案
函数的来源 copy
位于 util
io
导入的模块的模块文件,mod.rs
.因为 DEFAULT_BUF_SIZE
是 defined在 mod.rs
中,您需要通过 util.rs
中的 super
访问它。
关于rust - 了解 Rust 中 "super"关键字的具体用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125006/