title: Rust: How to fix borrowed value does not live long enough
body: <p>I have simple client/server application. I am receiving message on the server side from client but I want to send that response to the channel from server to other file and I am receiving error  &quot;borrowed value does not live long enough&quot;.</p>
  <p>I have searched in the stack overflow for similar previous questions but not getting enough understanding of lifetime. Is there a good documentation    or if simple example available on this topic?</p>
  <p>For now if someone can help me to fix this code (may be edit the portion of code which needs to fix) that would be helpful.</p>
  <p>Thanks in advance.</p>
  <h1>Server side:</h1>
  <pre><code>use std::os::unix::net::UnixDatagram;
  use std::path::Path;
  
  fn unlink_socket (path: impl AsRef&lt;Path&gt;) {
      let path = path.as_ref();
      if Path::new(path).exists() {
          let result = std::fs::remove_file(path);
          match result {
              Err(e) =&gt; {
                  println!(&quot;Couldn't remove the file: {:?}&quot;, e);
              },
              _ =&gt; {}
          }
      }
  }
  
  pub fn tcp_datagram_server() {
      pub static FILE_PATH: &amp;'static str = &quot;/tmp/datagram.sock&quot;;
      let (tx, rx) = mpsc::channel();
      let mut buf = vec![0; 1024];
      unlink_socket(FILE_PATH);
      let socket = match UnixDatagram::bind(FILE_PATH) {
          Ok(socket) =&gt; socket,
          Err(e) =&gt; {
              println!(&quot;Couldn't bind: {:?}&quot;, e);
              return;
          }
      };
      println!(&quot;Waiting for client to connect...&quot;);
      loop {
          let received_bytes = socket.recv(buf.as_mut_slice()).expect(&quot;recv function failed&quot;);
          println!(&quot;Received {:?}&quot;, received_bytes);
          let received_message = from_utf8(buf.as_slice()).expect(&quot;utf-8 convert failed&quot;);
          tx.clone().send(received_message);
      }
  }
  
  fn main() {
     tcp_datagram_server();
  }
  </code></pre>
  <h1>client side:</h1>
  <pre><code>use std::sync::mpsc;
  use std::os::unix::net::UnixDatagram;
  use std::path::Path;
  use std::io::prelude::*;
  
  pub fn tcp_datagram_client() {
      pub static FILE_PATH: &amp;'static str = &quot;/tmp/datagram.sock&quot;;
      let socket = UnixDatagram::unbound().unwrap();
      match socket.connect(FILE_PATH) {
          Ok(socket) =&gt; socket,
          Err(e) =&gt; {
              println!(&quot;Couldn't connect: {:?}&quot;, e);
              return;
          }
      };
      println!(&quot;TCP client Connected to TCP Server {:?}&quot;, socket);
      loop {
          socket.send(b&quot;Hello from client to server&quot;).expect(&quot;recv function failed&quot;);
      }
  }
  
  fn main() {
     tcp_datagram_client();
  }
  </code></pre>
  <p>Error I am getting</p>
  <pre><code>error[E0597]: `buf` does not live long enough
    --&gt; src/unix_datagram_server.rs:38:42
     |
  38 |         let received_message = from_utf8(buf.as_slice()).expect(&quot;utf-8 convert failed&quot;);
     |                                          ^^^ borrowed value does not live long enough
  ...
  41 | }
     | -
     | |
     | `buf` dropped here while still borrowed
     | borrow might be used here, when `tx` is dropped and runs the `Drop` code for type `std::sync::mpsc::Sender`
     |
     = note: values in a scope are dropped in the opposite order they are defined
  
  error: aborting due to previous error; 8 warnings emitted
  </code></pre>