diff options
| author | kennytm <kennytm@gmail.com> | 2018-09-07 13:47:05 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-09-07 15:26:26 +0800 |
| commit | 846a0bd1b92c7b3db112bfc9677c32e0976fb0cc (patch) | |
| tree | 39cde32ff93765a53fdc05d01494afa8e6a9ed72 /src/libcore | |
| parent | 9804689b587c698e718ccdf91d7dbed4cd3dea50 (diff) | |
| parent | 9d440d578d3ec724cae93d9fac3a2701724e780a (diff) | |
| download | rust-846a0bd1b92c7b3db112bfc9677c32e0976fb0cc.tar.gz rust-846a0bd1b92c7b3db112bfc9677c32e0976fb0cc.zip | |
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
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/macros.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 0032bedc7ed..a0c87f13e5d 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -349,6 +349,26 @@ macro_rules! try { /// write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt /// 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] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! write { |
