Unexpected print order

use ferris_says::say;
use std::io::{stdout, BufWriter};

fn f()
{
	print!("hello\n");
}

fn main()
{
	let stdout = stdout();
	let message = String::from("Hello fellow kids!");
	let width = message.chars().count();

	let mut writer = BufWriter::new(stdout.lock());
	say(message.as_bytes(), width, &mut writer).unwrap();
	f();
}

This is the standard first project example with an extra added function that just prints "hello" and a newline

Expected output :

 ____________________
< Hello fellow kids! >
 --------------------
        \
         \
            _~^~^~_
        \) /  o o  \ (/
          '_   -   _'
          / '-----' \

hello

Actual output :

hello
 ____________________
< Hello fellow kids! >
 --------------------
        \
         \
            _~^~^~_
        \) /  o o  \ (/
          '_   -   _'
          / '-----' \

I don't understand why this is

Add a call to flush. The text is currently being printed in the destructor of BufWriter, which will attempt to flush any data in the buffer.

3 Likes