How to Build a TCP Server in Rust

Build a basic TCP server in Rust using TcpListener to bind to a port and handle incoming connections.

Use std::net::TcpListener to bind to an address and iterate over incoming connections in a loop. This minimal example creates a server listening on localhost port 7878 that accepts connections and prints a confirmation message.

use std::net::TcpListener;

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

    for stream in listener.incoming() {
        let stream = stream.unwrap();
        println!("Connection established!");
    }
}