UDP trade-offs
Quick answer
UDP sends separate datagrams without guaranteeing delivery, order or uniqueness. It fits protocols that can tolerate loss or implement their own reliability rules.
Preserve message boundaries
A receive returns one datagram; an undersized buffer can truncate it. Unlike TCP, the next receive does not finish the truncated datagram. An application can include sequence numbers to reject older updates, but sequence wraparound, duplication and missing messages still require a policy. Use bounded payloads and timeouts; do not assume that sending means receiving.
use std::{net::UdpSocket, time::Duration};fn main() -> std::io::Result<()> { let r = UdpSocket::bind("127.0.0.1:0")?; let s = UdpSocket::bind("127.0.0.1:0")?; r.set_read_timeout(Some(Duration::from_secs(2)))?; s.send_to(b"hi", r.local_addr()?)?; let mut buf = [0; 32]; let (n, _) = r.recv_from(&mut buf)?; println!("{}", String::from_utf8_lossy(&buf[..n])); Ok(())}Manual local Rust/Cargo candidate; not executed. Expected behavior:
hi (if local datagram arrives before timeout)Only the received bytes are meaningful.
fn main() { let mut latest = 0; for seq in [3, 2, 3, 5] { if seq > latest { latest = seq; println!("{seq}"); } }}Expected stdout (not executed):
35This toy session assumes positive, non-wrapping sequence numbers.
Common mistake
Do not use the snapshot rule for financial or other operations that must not be lost.
Try it yourself
Add a counter of duplicate or stale snapshots and explain what happens to sequence 4.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
UDP trade-offs — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A UDP datagram is larger than the receive buffer. Can the next receive read the remainder?
UDP trade-offs — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{net::UdpSocket, time::Duration}; fn main() -> std::io::Result<()> { let r = UdpSocket::bind("127.0.0.1:0")?; let s = UdpSocket::bind("127.0.0.1:0")?; r.set_read_timeout(Some(Duration::from_secs(2)))?; s.send_to(b"hi", r.local_addr()?)?; let mut buf = [0; 32]; let (n, _) = r.recv_from(&mut buf)?; println!("{}", String::from_utf8_lossy(&buf[..n])); Ok(()) } ``` Which outcome or interpretation best matches this example?
UDP trade-offs — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let mut latest = 0; for seq in [3, 2, 3, 5] { if seq > latest { latest = seq; println!("{seq}"); } } } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.