Coming to Rust from C++

Key Differences

Rust ensures memory safety and concurrency without a garbage collector by using ownership and borrowing rules enforced at compile time.

Rust differs from C++ primarily by enforcing memory safety through ownership and borrowing, eliminating manual memory management and data races at compile time. Unlike C++, Rust uses a type system that distinguishes between ownership and references, preventing undefined behavior without a garbage collector.

fn main() {
    let listener = std::net::TcpListener::bind("127.0.0.1:7878").unwrap();
    for stream in listener.incoming() {
        let stream = stream.unwrap();
        handle_connection(stream);
    }
}

fn handle_connection(mut stream: std::net::TcpStream) {
    let response = "HTTP/1.1 200 OK\r\n\r\n";
    stream.write_all(response.as_bytes()).unwrap();
}