不能借来可变的...,因为它也被当作不可变借来的

问题描述 投票:-1回答:2

使用以下代码:

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
2个回答
0
投票

在循环中,您正在填充缓冲区,该缓冲区在连接之间共享。并且在每个连接处您都将其拆分。

您只想为当前连接拆分:

fn inicializar(receptor: TcpListener) {
    for recibido in receptor.incoming() {
        let recibido = recibido.expect("Unable to accept");
        let mut lector_buffer = BufReader::new(recibido);
        let mut respuesta = String::new();
        lector_buffer
            .read_line(&mut respuesta)
            .expect("could not read");
        peticion: Vec<&str> = respuesta.split_whitespace().collect();
        println!("quote es {}", peticion[0]);
    }
}

如果想将字符串保留在循环之外,则可能要使用String的实例,而不只是&str的实例(因为它们是指针,必须指向保留的对象)。

这可能类似于

fn inicializar(receptor: TcpListener) {
    let mut peticions: Vec<Vec<String>> = Vec::new();
    for recibido in receptor.incoming() {
        let recibido = recibido.expect("Unable to accept");
        let mut lector_buffer = BufReader::new(recibido);
        let mut respuesta = String::new();
        lector_buffer
            .read_line(&mut respuesta)
            .expect("could not read");
        let peticion: Vec<String> = respuesta
            .split_whitespace()
            .map(|s| s.to_string())
            .collect();
        println!("quote es {}", peticion[0]);
        peticions.push(peticion);
    }
    // you can use peticions here, or even return it
}

[此时,您需要通过定义结构来构造程序,以避免处理Vec中的Vec


-1
投票

感谢您的回答,这就是我想要的。我现在的其他问题是如何在for循环外使用“ peticion”变量。例如在休息后将您的内容打印出循环

fn inicializar (receptor: TcpListener)  {
    for recibido in receptor.incoming() {
        let recibido = recibido.expect("Unable to accept");
        let mut lector_buffer = BufReader::new(recibido);
        let mut respuesta = String::new();
        lector_buffer.read_line(&mut respuesta).expect("could not read");
        let peticion: Vec<&str> = respuesta.split_whitespace().collect();
        break
    }
    println!("quote es {}",peticion[0]);
}

 Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `peticion` in this scope
  --> src/main.rs:33:28
© www.soinside.com 2019 - 2024. All rights reserved.