- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在标准库中,BufWriter
声明如下:
pub struct BufWriter<W: Write> {
inner: Option<W>,
buf: Vec<u8>,
// #30888: If the inner writer panics in a call to write, we don't want to
// write the buffered data a second time in BufWriter's destructor. This
// flag tells the Drop impl if it should skip the flush.
panicked: bool,
}
正如文档告诉我们的那样,panicked
的存在是为了防止在展开时出现双重写入,我理解其背后的原因。内部编写器被包裹在 Option
中是否有类似的原因?我在代码中找到的唯一部分 Option
实际被清除的地方是 into_inner
但我看不出为什么这也不能用裸值来实现,因为 self
是按值取的。
最佳答案
原因很简单:BufWriter
实现了Drop
,并且您不能从实现了Drop
的类型中移出字段。就在 BufWriter::into_inner
返回之前,调用了 BufWriter::drop
,因为 BufWriter
被移入了函数的 self
参数,然后超出范围——这是标准的 RAII。
如果您能够移出一个字段,那么析构函数会发生什么?要么它根本不会被调用,这会令人惊讶,要么它会在某些包含无效值的字段中被调用,这可能会违反内存安全。
关于rust - 为什么 BufWriter 将内部 Write 包装在一个 Option 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48980186/
我正在尝试编写一个小的 Rust 程序,将/dev/ttyS0 上的行回显给发件人: fn echo_loop(device: &str) { let f = File::open(devic
有没有一种方法可以使用 Writer 并将写入此 writer 的内容取回?像 BufWriterReader 这样的东西?我试过这个,但没有编译: #[test] fn write_to_json_
BufWriter 有一个不可访问的私有(private)字段buf。我如何知道其中当前包含的数据的大小? 最佳答案 你不能(检查 the source to be sure )。内部 buf 没有以
看完 std::io::BufReader 文档,我不确定如何最好地传递 BufReader功能之间。允许多种排列,但哪种排列最好? 我有一个接受文件的函数: use std::{fs::File,
我用 Rust 编写了一个 echo 服务器和客户端。这是我的代码: 服务器: use std::net::{TcpListener, TcpStream}; use std::thread; use
我在写入文件时设置了 vim 以运行 Autoformat,但在对其进行编辑之前会定期忘记将扩展名添加到我的黑名单中。现在我无法保存编辑,因为自动格式化弄乱了缩进。有没有不运行 BufWrite 脚本
我想要一个 TcpStream由一个 BufReader 共享和一个 BufWriter ,我在以下地方找到了解决方案: If BufReader takes ownership of a strea
在标准库中,BufWriter声明如下: pub struct BufWriter { inner: Option, buf: Vec, // #30888: If the i
该用例由全世界所有 (vim) 开发人员共享(恕我直言):我们想在每个写入操作上更新一个标签和一个时间戳源文件。在 Barry Arthur 的帮助下,我的尝试是这样的: 每次写入更新的字符串示例 #
我是一名优秀的程序员,十分优秀!