【发布时间】:2021-12-04 16:29:12
【问题描述】:
我是 Rust 的新手,如果这个问题看起来不合适或很愚蠢,很抱歉。
我正在尝试从 TCP 流 stream 读取数据并使用 let (sender_buff, receiver_buff) = ::std::sync::mpsc::channel(); 将其发送到另一个线程。
这是我的代码:
const FRAME_SIZE:usize = 256;
let mut raw_bytes = [0 as u8; FRAME_SIZE];
let mut time_out = false;
while !time_out {
if stream.read_exact(&mut raw_bytes).is_ok() {
// send in the channel
sender_buff.send(&mut raw_bytes).ok();
}
但是它没有编译:
error[E0499]: cannot borrow `raw_bytes` as mutable more than once at a time
我不知道如何解决这个问题,有什么想法吗? 谢谢。
【问题讨论】:
-
您可能想要克隆您发送到频道的帧。否则(如果是 C),您将发送缓冲区,并在下一次迭代中立即开始覆盖缓冲区,因此接收线程可以接收任何内容。 (因为它是生锈的,它告诉你你做错了什么)
-
为每个新帧分配一个新缓冲区也应该有效。