Skip to lesson content
navan✳THE BUILD LABRUST
0/53 complete
BUILD LAB / RUST/ ASYNC & SYSTEM BOUNDARIES
BUILD NOTE 47 / 53

Pick the right conversation.

Separate byte streams, request/response messages, and persistent sessions.

BY NAVAN SAMI◷ 9 MIN READADVANCEDRUST 2024

01 Understand the contract

TCP provides an ordered byte stream, not application message boundaries. HTTP defines request and response semantics over a transport. WebSocket begins with an HTTP upgrade and then supports bidirectional framed messages over a persistent connection. A single read may contain only part of an application message, so define framing or use a protocol library. The cookbook uses Axum for HTTP and WebSocket framing rather than inventing an HTTP parser. Bind local practice servers to loopback so their audience matches the exercise.

02 Make it concrete

This is a complete standard-library example. Use Rust stable with the 2024 edition. Run it locally using the Rust workbench.

lessons/networking/src/main.rs
use std::net::SocketAddr;
fn main() {
    let address: SocketAddr = "127.0.0.1:3000".parse().unwrap();
    println!(
        "loopback={}, port={}",
        address.ip().is_loopback(),
        address.port()
    );
    let payload = "hello";
    println!("payload bytes={}", payload.len());
}
↳ EXPECTED OUTPUT
loopback=true, port=3000
payload bytes=5
  1. 1

    SocketAddr separates address and port.

  2. 2

    Loopback restricts access to the local machine.

  3. 3

    A payload length is a byte count, not a TCP packet boundary.

From the extracted rust-workbench folder
cargo run -p lesson_networking

03 Mind the boundary

04 Your next build

A SMALL MISSION

Parse an invalid address without panicking and test a valid loopback endpoint.

Reveal one possible solution +
solutions/networking/src/main.rs
use std::net::SocketAddr;
fn main() {
    assert!("invalid".parse::<SocketAddr>().is_err());
    let a: SocketAddr = "127.0.0.1:9000".parse().unwrap();
    assert!(a.ip().is_loopback());
}

05 Check your reasoning

QUICK SIGNAL CHECK

Does TCP preserve application message boundaries?

ONE MORE CONTRACT YOU CAN EXPLAIN.

Understand it.
Then make it yours.

Run the example, try your own input, and explain the boundary. Log the lesson when the idea makes sense.

Built with curiosity. By Navan Sami. ↗Rust · Make the contract clear. Then build.