Help to understand building web server

The following code comes from The Rust Book:

use std::{
    io::{prelude::*, BufReader},
    net::{TcpListener, TcpStream},

fn main() {
    let listener = TcpListener::bind("").unwrap();

    for stream in listener.incoming() {
        let stream = stream.unwrap();


fn handle_connection(mut stream: TcpStream) {
    let buf_reader = BufReader::new(&mut stream);
    let request_line = buf_reader.lines().next().unwrap().unwrap();

    let (status_line, filename) = if request_line == "GET / HTTP/1.1" {
        ("HTTP/1.1 200 OK", "hello.html")
    } else {
        ("HTTP/1.1 404 NOT FOUND", "404.html")

    let contents = fs::read_to_string(filename).unwrap();
    let length = contents.len();

    let response =
        format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");


And I had the "404.html" like this:

    <label for="uname">Choose a username: </label>
    <input type="text" id="uname" name="name" />

How can I get the input string(in the 4th line in the file) from 404.html, and print it in the main funcion in the

The form element will send another request to the server when the user hits the submit button[1]. You need to set up the connection handler to be able to tell when you're receiving that request as opposed to another, and then parse out the data (which could be in a couple different formats depending on how you configure the form element).

You can return the value from the handler function to print it in main.

The MDN page on the form element may be useful if you aren't familiar with HTML already

  1. I don't remember if <button>s automatically becomes submit buttons inside a form ↩︎

1 Like

Thanks for your reply. Can I ask what crates I can use to get it down? I found there are many crates like Rocket, yew, web_sys and so on, they are created for WasmAssembly. But I don't need WasmAssembly, I thought the situation I am meeting is much more simpler.

yew is a frontend framework, working in the same way as all the famous JS frameworks like React, Vue, Svelte, etc. yew runs in the browser of the user (compiled as web assembly), rendering a webpage.

web_sys are raw bindings for the browser APIs. yew relies on web_sys to access the browser APIs, i.e. for manipulating the DOM.

rocket is a web server. It runs on your server (no web assembly involved here, unless you are using a web assembly runtime on your server, like wasmtime or wasmer), serving data to a client (i.e. you typing the URL to your server in the address bar of your browser). Now the code you provided in your question is a very basic web server build with functionality from the standard library. Frameworks like rocket abstract a lot of boilerplate away from your so you can focus on writing the logic of your application, rather than having to worry about all the technical stuff, like parsing HTTP requests, TLS, routing, HTTP headers, multithreading, etc. There are many web server frameworks out there to choose from, like rocket, actix-web, axum or warp. For a small discussion which one we think is best, see this recent topic:


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.