title: Rust struct field mutable for socket
body: <p>I am trying to get started with Rust, and was trying to put some pieces together and have a "Server" instance which contains a Vector of "Clients" where each of them have a Socket.</p>
  
  <p>I understand that in Rust the Socket or TcpStream needs to be mutable, and that I need to borrow the reference in order to keep the scope after the Client instantiation in my main loop.</p>
  
  <p>But I faced the issue that my TcpStream field may not be mutable in the Client struct. So I am not sure my approach is correct, but I tried to solve this using the lifetime parameter &lt;'a>, however this leads me to another problem where my "Client" inside "Server" does not pass the &lt;'a> lifetime parameter.</p>
  
  <p>Can someone help me solve this problem or show me the correct approach to this problem / solution?</p>
  
  <p>Thanks.</p>
  
  <pre><code>use std::net::*;
  use std::io::Write;
  use std::thread;
  use std::time::Duration;
  
  struct Client&lt;'a&gt; {
      socket: &amp;'a mut TcpStream,
      addr: SocketAddr
  }
  
  struct Server {
      clients: Vec&lt;Box&lt;Client&gt;&gt;
  }
  
  impl Server {
      pub fn new() -&gt; Server {
          Server{clients: Vec::new()}
      }
  
      fn write(&amp;self, stream: &amp;mut TcpStream) {
          let mut counter: u32 = 0;
          counter += 1;
          stream.write(counter.to_string().as_bytes()).unwrap();
          thread::sleep(Duration::from_secs(1));
      }
  
      fn client_thread(&amp;self, client: &amp;mut Client) {
          self.write(&amp;mut client.socket);
      }
  
      fn add_client(&amp;self, socket: &amp;mut TcpStream, addr: SocketAddr) {
          let mut client = Client {
              socket: socket,
              addr: addr
          };
  
          self.clients.push(Box::new(client));
  
          self.client_thread(&amp;mut client);
      }
  
      pub fn server_loop(&amp;self) {
          let listener = TcpListener::bind("127.0.0.1:5001").unwrap();
  
          loop {
              match listener.accept() {
                  Ok((mut socket, addr)) =&gt; {
                      println!("new client: {:?}", addr);
  
                      thread::spawn(move || loop {
                          self.add_client(&amp;mut socket, addr);
                      });
                  },
                  Err(e) =&gt; println!("couldn't get client: {:?}", e),
              }
          }
      }
  }
  
  fn main() {
      let mut server = Server::new();
      server.server_loop();
  }
  </code></pre>
  
  <p>Update:</p>
  
  <p>The current error message is:</p>
  
  <pre><code> clients: Vec&lt;Box&lt;Client&gt;&gt;
                    ^^^^^^ expected lifetime parameter
  </code></pre>
  
  <p>Update 2:</p>
  
  <p>Now I think the solution is a little bit better / closer to the goal. But I still have a problem with the thread:spawn outside static context.</p>
  
  <pre><code>use std::net::*;
  use std::io::Write;
  use std::thread;
  
  struct Client {
      socket: TcpStream
  }
  
  struct Server {
      clients: Vec&lt;Box&lt;Client&gt;&gt;
  }
  
  impl Server {
      fn new() -&gt; Server {
          Server{clients: vec![]}
      }
  
      fn write(&amp;mut self, stream: &amp;mut TcpStream) {
          let mut counter: u32 = 0;
          stream.write(counter.to_string().as_bytes()).unwrap();
      }
  
      fn client_loop(&amp;mut self, client: &amp;mut Client) {
          loop {
              self.write(&amp;mut client.socket);
          }
      }
  
      fn add_client(&amp;mut self, s: TcpStream) {
          let mut client = Client{
              socket: s
          };
  
          self.clients.push(Box::new(client));
  
          println!("New client: {}", client.socket.peer_addr().unwrap());
  
          thread::spawn(move || {
              self.client_loop(&amp;mut client);
          });
      }
  
      pub fn server_loop(&amp;mut self) {
          let listener = TcpListener::bind("127.0.0.1:5001").unwrap();
  
          loop {
              match listener.accept() {
                  Ok((socket, _addr)) =&gt; {
                      self.add_client(socket);
                  },
                  Err(e) =&gt; println!("Couldn't get client: {}", e),
              }
          }
      }
  }
  
  fn main() {
      let mut server = Server::new();
      server.server_loop();
  }
  </code></pre>
  
  <p>error[E0477]: the type <code>[closure@src/main.rs:38:23: 40:10 self:&amp;mut Server, client:Client]</code> does not fulfill the required lifetime</p>
  
  <pre><code>  --&gt; src/main.rs:38:9
     |
  38 |         thread::spawn(move || {
     |         ^^^^^^^^^^^^^
     |
     = note: type must satisfy the static lifetime
  </code></pre>