Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nickdesaulniers opened this issue Jan 14, 2014 · 1 comment

Comments

@nickdesaulniers
Copy link

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.

@huonw
Copy link
Member

huonw commented Jan 14, 2014

Closing as a dupe of #10617.

@huonw huonw closed this as completed Jan 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants