Skip to content

Commit 846a0bd

Browse files
committed
Rollup merge of rust-lang#53299 - MagnumOpus21:fix-macro-write, r=steveklabnik
Updated core/macros.rs to note it works in a no_std environment. Fixes rust-lang#45797 This PR updates the documentation of `write!` to note it works in a `no_std` environment, and adds an example to showcase this. In a `no_std` environment, the author of the code is responsible for the implementation of the `Write` trait. This example will work out of the box with `no_std`, but the implementation of `Write` is expected to be provided by the user. r? @steveklabnik
2 parents 9804689 + 9d440d5 commit 846a0bd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/macros.rs

+20
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,26 @@ macro_rules! try {
349349
/// write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt
350350
/// assert_eq!(v, b"s = \"abc 123\"");
351351
/// ```
352+
///
353+
/// Note: This macro can be used in `no_std` setups as well
354+
/// In a `no_std` setup you are responsible for the
355+
/// implementation details of the components.
356+
///
357+
/// ```no_run
358+
/// # extern crate core;
359+
/// use core::fmt::Write;
360+
///
361+
/// struct Example;
362+
///
363+
/// impl Write for Example {
364+
/// fn write_str(&mut self, _s: &str) -> core::fmt::Result {
365+
/// unimplemented!();
366+
/// }
367+
/// }
368+
///
369+
/// let mut m = Example{};
370+
/// write!(&mut m, "Hello World").expect("Not written");
371+
/// ```
352372
#[macro_export]
353373
#[stable(feature = "rust1", since = "1.0.0")]
354374
macro_rules! write {

0 commit comments

Comments
 (0)