std-method

TCP_NODELAY option

Disable Nagle’s algorithm on the socket when a measured latency requirement justifies it. This setting neither preserves write boundaries nor guarantees lower latency for every workload. Expected stdout (not executed): true false Common mistake: Treating TCP_NODELAY as a universal performance improvement or a framing mechanism.

Formal Syntax & Structure

rust syntaxrust
stream.set_nodelay(true); stream.nodelay()

Working Example

Example Snippetrust
fn main() {    use std::{net::{Ipv4Addr, TcpListener, TcpStream}, time::Duration};    let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();    let mut client = TcpStream::connect_timeout(&listener.local_addr().unwrap(), Duration::from_secs(2)).unwrap();    let (mut server, _) = listener.accept().unwrap();    for s in [&client, &server] {        s.set_read_timeout(Some(Duration::from_secs(2))).unwrap();        s.set_write_timeout(Some(Duration::from_secs(2))).unwrap();    }    client.set_nodelay(true).unwrap();    println!("{}", client.nodelay().unwrap());    client.set_nodelay(false).unwrap();    println!("{}", client.nodelay().unwrap());}
Rust Reference