Read input from another program

Hi there,
I'm trying to implement some port from Erlang to Rust.
Here's my Rust code:

    use std::io;
    fn main() {
        println!("{}", "test0");
        loop {
            let mut guess = String::new();
                .read_line(&mut guess)
            println!("You guess {}", guess);

Erlang code:

    P = open_port({spawn, main}, []).
    P ! {self(), {command, "aaa"}}.

This flush() shows me
Shell got {#Port<0.6>,{data,"test0\ntest1\n"}}
which means nothing comes into my io::stdin()...

That's probably a really dumb question, but I could not get how to read data from stdin (file descriptor 0) in Rust in this case (meaning that data is not a user's input).

Try adding a newline to the string you write to stdin if the Erlang code doesn't already implicitly do so. The read_line function reads data until it sees one, so if one is never written to stdin it never finishes (and thus "test2" is never printed).

Thanks a lot, that was the issue!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.