Puzzle 2Non-standard Input
 use​ ​std​::​io​::stdin;
 
 fn​ ​main​() {
  println!(​"What is 3+2? Type your answer and press enter."​);
 let​ ​mut​ input = ​String​::​new​();
 stdin​()
 .read_line​(&​mut​ input)
 .expect​(​"Unable to read standard input"​);
 
 if​ input == ​"5"​ {
  println!(​"Correct!"​);
  } ​else​ {
  println!(​"Incorrect!"​);
  }
 }

Guess the Output

images/aside-icons/important.png

Try to guess what the output is before moving to the next page.

images/hline.png

The program’s interaction will look something like the following:

<= What is ...

Get Rust Brain Teasers now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.