【发布时间】:2019-10-29 10:16:51
【问题描述】:
使用以下代码:
use std::{
io::{BufRead, BufReader},
net::TcpListener,
};
fn inicializar(receptor: TcpListener) {
let mut peticion: Vec<&str> = Vec::new();
let mut respuesta = String::new();
let mut lector_buffer;
for recibido in receptor.incoming() {
let recibido = recibido.expect("Unable to accept");
lector_buffer = BufReader::new(recibido);
lector_buffer
.read_line(&mut respuesta)
.expect("could not read");
peticion = respuesta.split_whitespace().collect();
println!("quote es {}", peticion[0]);
}
}
产生此错误:
error[E0502]: cannot borrow `respuesta` as mutable because it is also borrowed as immutable
--> src/lib.rs:12:24
|
12 | .read_line(&mut respuesta)
| ^^^^^^^^^^^^^^ mutable borrow occurs here
13 | .expect("could not read");
14 | peticion = respuesta.split_whitespace().collect();
| -------- --------- immutable borrow occurs here
| |
| immutable borrow might be used here, when `peticion` is dropped and runs the `Drop` code for type `std::vec::Vec`
我怎样才能让它工作?
【问题讨论】:
标签: rust