Simple Rust Webserver
Learn to love rustfmt:


Spaces go between comma-separated items:

-use std::io::{Read, Write,BufReader, BufRead};
+use std::io::{Read, Write, BufReader, BufRead};




-let response = format!("{}{}{}{}","HTTP/1.1 ",status," OK\n\n",res);
+let response = format!("{}{}{}{}", "HTTP/1.1 ", status, " OK\n\n", res);

Spaces go before braces:

-fn main(){
+fn main() {




-loop{
+loop {

Colons attach on the left side of type definitions:

-fn send_response(mut stream: TcpStream, res :&amp;str, status :u32){
+fn send_response(mut stream: TcpStream, res: &amp;str, status: u32) {


Pay attention to compiler warnings. One of the entire purposes of using a compiled language is to have the compiler watch your back. If you ignore it, then what good is it?

warning: unused result which must be used, #[warn(unused_must_use)] on by default
  --&gt; src/main.rs:19:5
   |
19 |     reader.read_line(&amp;mut lines);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused result which must be used, #[warn(unused_must_use)] on by default
  --&gt; src/main.rs:35:9
   |
35 |         file.read_to_string(&amp;mut response);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    

Introduce more functions to break up the distinct parts of read_request. From my inspection, there's a part that parses the request, a part that handles the request by reading a file, and a part that responds (which already exists). Note that doing this contrains the number of lines of code a variable is mutable and avoids the need to recover the inner TcpStream from the BufReader.
vec_line is poorly named; it's not a Vec. I am happy to see your use of nth; many people would collect into a Vec, which is inefficient.
There's no reason to convert the result of nth into a String, you can call replace on a &amp;str equally as well.
Avoid using mut just to be able to reassign a variable (e.g. the result of replace). Instead, just bind the variable a second time. This reduces the amount and scope of mutability.
requested_page == "" should be requested_page.is_empty()
Why would you convert a String to a String using to_string? Just take a reference to the existing string.
There's no need to create a string to write to the stream; just write! directly to the stream.
The code always prints "OK", regardless of the status.
Checking if a file exists before reading it introduces a race where the file can be deleted between the check and the read. It's better to open the file and check any resulting error.
Instead of creating mutable variables (response, status) with defaults, consider only setting them in the appropriate branches. Again, this reduces the number of mutable variables and the scopes of mutability.
Once response and status are moved to a function, you can just return them directly.
read_request does more than just read, so it should be renamed.
The current code doesn't require a TcpStream except in the outermost loop. It only needs anything that implements Read and Write. Using generics will allow you to more easily test those functions in isolation.




use std::io::{self, Read, Write, BufReader, BufRead};
use std::net::TcpListener;
use std::fs::File;

fn main() {
    loop {
        let listener = TcpListener::bind("localhost:8000").unwrap();
        let stream = listener.accept().unwrap().0;
        handle_request(stream);
    }
}

fn handle_request&lt;S&gt;(mut stream: S)
    where S: Read + Write
{
    let requested_page = parse_request(&amp;mut stream);
    let (response, status) = read_file(&amp;requested_page);
    send_response(stream, &amp;response, status);
}

fn parse_request&lt;R&gt;(stream: R) -&gt; String
    where R: Read
{
    let mut line = String::new();

    let mut reader = BufReader::new(stream);
    reader.read_line(&amp;mut line).unwrap();

    let mut words = line.split_whitespace();
    let requested_page = words.nth(1).unwrap();
    let requested_page = requested_page.replace("/", "");

    if requested_page.is_empty() {
        String::from("index.html")
    } else {
        requested_page
    }
}

fn read_file(requested_page: &amp;str) -&gt; (String, u32) {
    match File::open(&amp;requested_page) {
        Ok(mut file) =&gt; {
            let mut s = String::new();
            file.read_to_string(&amp;mut s).unwrap();
            (s, 200)
        }
        Err(ref e) if e.kind() == io::ErrorKind::NotFound =&gt; (String::from("Not Found!"), 404),
        Err(e) =&gt; panic!("Unable to open file: {}", e),
    }
}

fn send_response&lt;W&gt;(mut stream: W, res: &amp;str, status: u32)
    where W: Write
{
    write!(&amp;mut stream, "{}{}{}{}", "HTTP/1.1 ", status, " OK\n\n", res).unwrap();
}


There are way too many uses of unwrap. At the very least, use expect so you have some clue about what caused the program to terminate. Even better, use Result for error handling.

As a basic step, returning a Box&lt;Error&gt; is extremely simple and allows you to consolidate your error handling:

use std::io::{self, Read, Write, BufReader, BufRead};
use std::net::TcpListener;
use std::fs::File;

type Error = Box&lt;std::error::Error&gt;;
type Result&lt;T&gt; = std::result::Result&lt;T, Error&gt;;

fn main() {
    loop {
        if let Err(e) = handle_one("localhost:8000") {
            panic!("An error occurred, I'm exiting! {}", e);
        }
    }
}

fn handle_one(address: &amp;str) -&gt; Result&lt;()&gt; {
    let listener = TcpListener::bind(address)?;
    let stream = listener.accept()?.0;
    handle_request(stream)
}

fn handle_request&lt;S&gt;(mut stream: S) -&gt; Result&lt;()&gt;
    where S: Read + Write
{
    let requested_page = parse_request(&amp;mut stream)?;
    let (response, status) = read_file(&amp;requested_page)?;
    send_response(stream, &amp;response, status)
}

fn parse_request&lt;R&gt;(stream: R) -&gt; Result&lt;String&gt;
    where R: Read,
{
    let mut line = String::new();

    let mut reader = BufReader::new(stream);
    reader.read_line(&amp;mut line)?;

    let mut words = line.split_whitespace();
    let requested_page = words.nth(1).ok_or("request missing path")?;
    let requested_page = requested_page.replace("/", "");

    if requested_page.is_empty() {
        Ok(String::from("index.html"))
    } else {
        Ok(requested_page)
    }
}

fn read_file(requested_page: &amp;str) -&gt; Result&lt;(String, u32)&gt; {
    match File::open(&amp;requested_page) {
        Ok(mut file) =&gt; {
            let mut s = String::new();
            file.read_to_string(&amp;mut s)?;
            Ok((s, 200))
        },
        Err(ref e) if e.kind() == io::ErrorKind::NotFound =&gt; {
            Ok((String::from("Not Found!"), 404))
        }
        Err(e) =&gt; Err(e.into()),
    }
}

fn send_response&lt;W&gt;(mut stream: W, res: &amp;str, status: u32) -&gt; Result&lt;()&gt;
    where W: Write,
{
    write!(&amp;mut stream, "{}{}{}{}", "HTTP/1.1 ", status, " OK\n\n", res)?;
    Ok(())
}