@@ -348,6 +348,21 @@ macro_rules! try {
348
348
///
349
349
/// assert_eq!(w, b"testformatted arguments");
350
350
/// ```
351
+ ///
352
+ /// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects
353
+ /// implementing either, as objects do not typically implement both. However, the module must
354
+ /// import the traits qualified so their names do not conflict:
355
+ ///
356
+ /// ```
357
+ /// use std::fmt::Write as FmtWrite;
358
+ /// use std::io::Write as IoWrite;
359
+ ///
360
+ /// let mut s = String::new();
361
+ /// let mut v = Vec::new();
362
+ /// write!(&mut s, "{} {}", "abc", 123).unwrap(); // uses fmt::Write::write_fmt
363
+ /// write!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt
364
+ /// assert_eq!(v, b"s = \"abc 123\"");
365
+ /// ```
351
366
#[ macro_export]
352
367
#[ stable( feature = "core" , since = "1.6.0" ) ]
353
368
macro_rules! write {
@@ -391,6 +406,21 @@ macro_rules! write {
391
406
///
392
407
/// assert_eq!(&w[..], "test\nformatted arguments\n".as_bytes());
393
408
/// ```
409
+ ///
410
+ /// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects
411
+ /// implementing either, as objects do not typically implement both. However, the module must
412
+ /// import the traits qualified so their names do not conflict:
413
+ ///
414
+ /// ```
415
+ /// use std::fmt::Write as FmtWrite;
416
+ /// use std::io::Write as IoWrite;
417
+ ///
418
+ /// let mut s = String::new();
419
+ /// let mut v = Vec::new();
420
+ /// writeln!(&mut s, "{} {}", "abc", 123).unwrap(); // uses fmt::Write::write_fmt
421
+ /// writeln!(&mut v, "s = {:?}", s).unwrap(); // uses io::Write::write_fmt
422
+ /// assert_eq!(v, b"s = \"abc 123\\n\"\n");
423
+ /// ```
394
424
#[ macro_export]
395
425
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
396
426
macro_rules! writeln {
0 commit comments