diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index db0a6eff8367d..37cc71bff62b4 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -353,11 +353,15 @@ macro_rules! r#try { /// use std::fmt::Write as FmtWrite; /// use std::io::Write as IoWrite; /// -/// let mut s = String::new(); -/// let mut v = Vec::new(); -/// write!(&mut s, "{} {}", "abc", 123).unwrap(); // uses fmt::Write::write_fmt -/// write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt -/// assert_eq!(v, b"s = \"abc 123\""); +/// fn main() -> Result<(), Box> { +/// let mut s = String::new(); +/// let mut v = Vec::new(); +/// +/// write!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt +/// write!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt +/// assert_eq!(v, b"s = \"abc 123\""); +/// Ok(()) +/// } /// ``` /// /// Note: This macro can be used in `no_std` setups as well. @@ -399,14 +403,17 @@ macro_rules! write { /// # Examples /// /// ``` -/// use std::io::Write; +/// use std::io::{Write, Result}; /// -/// let mut w = Vec::new(); -/// writeln!(&mut w).unwrap(); -/// writeln!(&mut w, "test").unwrap(); -/// writeln!(&mut w, "formatted {}", "arguments").unwrap(); +/// fn main() -> Result<()> { +/// let mut w = Vec::new(); +/// writeln!(&mut w)?; +/// writeln!(&mut w, "test")?; +/// writeln!(&mut w, "formatted {}", "arguments")?; /// -/// assert_eq!(&w[..], "\ntest\nformatted arguments\n".as_bytes()); +/// assert_eq!(&w[..], "\ntest\nformatted arguments\n".as_bytes()); +/// Ok(()) +/// } /// ``` /// /// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects @@ -417,11 +424,15 @@ macro_rules! write { /// use std::fmt::Write as FmtWrite; /// use std::io::Write as IoWrite; /// -/// let mut s = String::new(); -/// let mut v = Vec::new(); -/// writeln!(&mut s, "{} {}", "abc", 123).unwrap(); // uses fmt::Write::write_fmt -/// writeln!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt -/// assert_eq!(v, b"s = \"abc 123\\n\"\n"); +/// fn main() -> Result<(), Box> { +/// let mut s = String::new(); +/// let mut v = Vec::new(); +/// +/// writeln!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt +/// writeln!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt +/// assert_eq!(v, b"s = \"abc 123\\n\"\n"); +/// Ok(()) +/// } /// ``` #[macro_export] #[stable(feature = "rust1", since = "1.0.0")]