Given the code below, how can I specifically test EOF? Rather, how can I distinguish between "there is nothing here" and "it exploded"?
match io::stdin().read_line() { Ok(l) => print!("{}", l), Err(_) => do_something_else(), }
From the documentation for read_line :
read_line
If successful, this function will return the total number of bytes read.If this function returns Ok(0) , the thread has reached EOF.
If successful, this function will return the total number of bytes read.
If this function returns Ok(0) , the thread has reached EOF.
Ok(0)
This means that we can verify the successful value of zero:
use std::io::{self, BufRead}; fn main() -> io::Result<()> { let mut empty: &[u8] = &[]; let mut buffer = String::new(); let bytes = empty.read_line(&mut buffer)?; if bytes == 0 { println!("EOF reached"); } Ok(()) }
Source: https://habr.com/ru/post/1209030/More articles:Is Mac OS X POSIX inconsistent? (Timer_settime) - unixdidRotateFromInterfaceOrientation fires twice during rotation - iosWhat are the / v1 / for URLs in Stripe API URLs? - versioningWhy am I getting an error when trying to use LaTeX in chart shortcuts - pythonui-router gets current state change path for google analytics - angularjsDjango, Python, and Class variables - pythonPyCharm docstrings associated with classes - pythonFix & line-height - cssThe project works locally, but crashes on Azure - asp.netFont Anomaly in FLTK Hello World - c ++All Articles