How to tell when connection is done sending bytes?

⚓ Rust    📅 2026-06-28    👤 surdeus    👁️ 2      

surdeus

Hello, I'm attempting to collect all the bytes into a String

    tokio::spawn(async move {
        let mut buf = [0; 5];
        let mut string = String::new();

        loop {
            // When all bytes are received, it seems to stop here and wait for more bytes.
            let n = match socket.read(&mut buf).await {
                Ok(n) => n,
                Err(e) => {
                    dbg!(e);
                    return;
                }
            };

            // An attempt to break the loop here.
            if n == 0 {
                break;
            }

            string.push_str(String::from_utf8(buf[..n].to_vec()).unwrap().as_str());
        }

        println!("{}", string);
    });

How can I tell when the bytes are done sending so that I can exit the loop?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed