1
Fork 0

Rollup merge of #53299 - MagnumOpus21:fix-macro-write, r=steveklabnik

Updated core/macros.rs to note it works in a no_std environment.

Fixes #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
This commit is contained in:
kennytm 2018-09-07 13:47:05 +08:00
commit 846a0bd1b9
No known key found for this signature in database
GPG key ID: FEF6C8051D0E013C

View file

@ -349,6 +349,26 @@ macro_rules! try {
/// write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt /// write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt
/// assert_eq!(v, b"s = \"abc 123\""); /// assert_eq!(v, b"s = \"abc 123\"");
/// ``` /// ```
///
/// Note: This macro can be used in `no_std` setups as well
/// In a `no_std` setup you are responsible for the
/// implementation details of the components.
///
/// ```no_run
/// # extern crate core;
/// use core::fmt::Write;
///
/// struct Example;
///
/// impl Write for Example {
/// fn write_str(&mut self, _s: &str) -> core::fmt::Result {
/// unimplemented!();
/// }
/// }
///
/// let mut m = Example{};
/// write!(&mut m, "Hello World").expect("Not written");
/// ```
#[macro_export] #[macro_export]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
macro_rules! write { macro_rules! write {