@@ -27,12 +27,23 @@ macro_rules! panic {
27
27
/// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted
28
28
/// immediately.
29
29
///
30
+ /// The `print!` macro will lock the standard output on each call. If you call
31
+ /// `print!` within a hot loop, this behavior may be the bottleneck of the loop.
32
+ /// To avoid this, lock stdout with [`io::stdout().lock()`][lock]:
33
+ /// ```
34
+ /// use std::io::{stdout, Write};
35
+ ///
36
+ /// let mut lock = stdout().lock();
37
+ /// write!(lock, "hello world").unwrap();
38
+ /// ```
39
+ ///
30
40
/// Use `print!` only for the primary output of your program. Use
31
41
/// [`eprint!`] instead to print error and progress messages.
32
42
///
33
43
/// [flush]: crate::io::Write::flush
34
44
/// [`println!`]: crate::println
35
45
/// [`eprint!`]: crate::eprint
46
+ /// [lock]: crate::io::Stdout
36
47
///
37
48
/// # Panics
38
49
///
@@ -75,11 +86,22 @@ macro_rules! print {
75
86
/// This macro uses the same syntax as [`format!`], but writes to the standard output instead.
76
87
/// See [`std::fmt`] for more information.
77
88
///
89
+ /// The `println!` macro will lock the standard output on each call. If you call
90
+ /// `println!` within a hot loop, this behavior may be the bottleneck of the loop.
91
+ /// To avoid this, lock stdout with [`io::stdout().lock()`][lock]:
92
+ /// ```
93
+ /// use std::io::{stdout, Write};
94
+ ///
95
+ /// let mut lock = stdout().lock();
96
+ /// writeln!(lock, "hello world").unwrap();
97
+ /// ```
98
+ ///
78
99
/// Use `println!` only for the primary output of your program. Use
79
100
/// [`eprintln!`] instead to print error and progress messages.
80
101
///
81
102
/// [`std::fmt`]: crate::fmt
82
103
/// [`eprintln!`]: crate::eprintln
104
+ /// [lock]: crate::io::Stdout
83
105
///
84
106
/// # Panics
85
107
///
0 commit comments