调用Read :: read时预期& mut [u8]

时间:2015-05-23 20:53:56

标签: rust

use std::io::Read;
use std::net::TcpListener;

struct Server<'a> {
    ip_addr: &'a str,
}

impl<'a> Server<'a> {
    fn receive(&self) {
        let mut received_message_buf: [u8; 100];

        let tcp_listener = TcpListener::bind(self.ip_addr).unwrap();
        tcp_listener.accept().unwrap().0.read(&received_message_buf);
    }
}

fn main() {}

我的类型不匹配:

<anon>:13:47: 13:68 error: mismatched types:
 expected `&mut [u8]`,
    found `&[u8; 100]`
(values differ in mutability) [E0308]
<anon>:13         tcp_listener.accept().unwrap().0.read(&received_message_buf);
                                                        ^~~~~~~~~~~~~~~~~~~~~

1 个答案:

答案 0 :(得分:3)

您需要使用&代替&mut

read()创建一个可变引用(因此&可以将内容放入缓冲区),而void Reset(int length){ this->~Array(); new(this) Array(length); } 创建一个不可变引用。