Skip to content

error: cannot borrow immutable captured outer variable in a heap closure as mutable #11528

Closed
@nickdesaulniers

Description

@nickdesaulniers
use std::io::{Listener, Acceptor};
use std::io::net::ip::{SocketAddr, Ipv4Addr};
use std::io::net::tcp::{TcpListener, TcpAcceptor, TcpStream};

fn main () {
  let addr: SocketAddr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 3000 };
  let listener: TcpListener = match TcpListener::bind(addr) {
    None => { error!("Failed to bind"); return; },
    Some(l) => l
  };

  let mut acceptor: TcpAcceptor = match listener.listen() {
    None => { error!("Failed to listen"); return; },
    Some(a) => a
  };

  let mut count: i32 = 0i32;

  loop {
    let mut stream: TcpStream = match acceptor.accept() {
      None => continue,
      Some(s) => s
    };

    do spawn {
      stream.write_str("hello world\n");
      count += 1;

      match stream.peer_name() {
        None => return,
        Some(p) => println(p.ip.to_str() + ":" + p.port.to_str())
      };

      match stream.socket_name() {
        None => return,
        Some(s) => println(s.ip.to_str() + ":" + s.port.to_str())
      };

      println!("{} connections", count);
    }
  }
}
test.rs:26:7: 26:13 error: cannot borrow immutable captured outer variable in a heap closure as mutable
test.rs:26       stream.write_str("hello world\n");

Why can't I close over the variable stream? Is there a way I can move it? @cmr and @dbaupp seem to think it's a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions