You build a simple HTTP server in Rust by creating a TCP listener, accepting connections in a loop, and writing a response to the socket stream. This approach uses only the standard library without external frameworks.
use std::net::{TcpListener, TcpStream};
use std::io::Write;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
let mut stream = stream.unwrap();
let response = "HTTP/1.1 200 OK\r\n\r\nHello, world!";
write!(stream, "{}", response).unwrap();
}
}