Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does code that requires user input not work in the Rust Playground?

Tags:

rust

I copy pasted the code from The Rust Programming Language under the heading "Processing a Guess" onto the Rust Playground. However, there is no prompt shown in the standard output. I have searched for this problem and I think that this may be one of the "limitations" that the help lists.

I have decided that I should use some software that will help me code without the Playground, but I do not have an IDE set up yet, and that is not part of my question anyhow.

What I have tried to do is copy paste the following code onto the Rust playground:

use std::io;

fn main() {
    println!("Guess the number!");

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin().read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {}", guess);
}

The result is that it will compile properly without any panic attacks, errors, or warnings, and print to the 'standard output' the following:

Guess the number!
Please input your guess.
You guessed: 

The information in Listing 2-1 in the book tells us that this code should be able to get a guess from the user and the print that guess (as a string).

My expectation is that I would be able to click on the line in the 'standard output' where it says:

You guessed: 

but when I click on the white-space to the right of the colon, and start typing, no new string appears.

Am I missing something crucial here?

like image 236
joshi001 Avatar asked Apr 17 '19 14:04

joshi001


1 Answers

The Playground does not currently accept user input via stdin. The stdin is effectively closed immediately, thus your program gets no input and moves on to the next line of code, printing the "guess", which is just the empty string.

that this may be one of the "limitations" that the help lists.

It is not, but probably should be.

Source: I am the creator of the playground.

like image 107
Shepmaster Avatar answered Nov 15 '22 09:11

Shepmaster