expression
Retry an interrupted read without confusing it with EOF
Interrupted means the operation was interrupted and can often be retried if no progress was reported. EOF is a successful zero-length read with a nonempty buffer on a stream. This mock uses a bounded number of attempts; production code should respect cancellation and deadlines. Expected stdout (not executed): A Common mistake: Treating Interrupted as EOF or retrying all errors forever. Run only in the isolated fixture runner, not a public code playground.
Formal Syntax & Structure
rust syntaxrust
match reader.read(buf) { Err(e) if e.kind() == ErrorKind::Interrupted => ... }Working Example
Example Snippetrust
use std::io::{self,Read,ErrorKind};struct Demo { interrupted: bool }impl Read for Demo { fn read(&mut self, b:&mut [u8]) -> io::Result<usize> { if b.is_empty() { return Ok(0); } if !self.interrupted { self.interrupted=true; return Err(ErrorKind::Interrupted.into()); } b[0]=b'A'; Ok(1) }}fn main() -> io::Result<()> { let mut reader=Demo{interrupted:false}; let mut b=[0;1]; reader.read_exact(&mut b)?; println!("{}",char::from(b[0])); Ok(())}