Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call to print!() only executes at the end of the program #78891

Closed
hboutou opened this issue Nov 9, 2020 · 2 comments
Closed

call to print!() only executes at the end of the program #78891

hboutou opened this issue Nov 9, 2020 · 2 comments

Comments

@hboutou
Copy link

hboutou commented Nov 9, 2020

cat /etc/os-release:

NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.12.1
PRETTY_NAME="Alpine Linux v3.12"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

rustc --version --verbose:

rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-musl
release: 1.47.0
LLVM version: 11.0

I tried this code:

use std::io;

fn main() {
    let mut user_input = String::new();
    print!("Enter input: ");
    io::stdin()
        .read_line(&mut user_input)
        .expect("failed to read line");
    println!("Your input: {}", user_input);
}

I expected the program to first print a string to stdout, read a string from stdin, then print again to stdout.

Instead, this happened:
The program reads from stdin first, then prints the two messages.

I noticed this only happens when using print!.
With println! the order is respected.

@ghost
Copy link

ghost commented Nov 9, 2020

I think this is #23818?

@jonas-schievink
Copy link
Contributor

Yeah, closing as a duplicate of that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants