Print statements display out of order

I was running a simple test program and discovered my print statements were being displayed out of order.

For example, at the end of my program my code prints the word "End". The output printed "End" followed by some text from an earlier print statement

Any explanation for this behavior?

It is difficult to say without seeing anything at all of the code you have, to understand what might be involved. Here is some speculation:

  • If you are using #[test] functions, they will run in an arbitrary order in multiple threads.
  • If you are mixing println!() and eprintln!() or dbg!() (more generally, stdout and stderr), depending on the environment, they might display out of order (e.g. the Rust Playground will display them entirely separately).

Please, show us at least part of your test program.

4 Likes

I think it's the buffering that's doing it.

You need to show a minimised code example.
Things that spring to mind are from drop ordering or threading.

1 Like

Buffering is a likely culprit. Are you using print! or println!? Standard out is buffered via a line break, which println! will include for you, whereas print! does not.

1 Like

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.