@@ -532,11 +532,9 @@ pub trait Read {
532
532
533
533
/// A trait for objects which are byte-oriented sinks.
534
534
///
535
- /// The `write` method will attempt to write some data into the object,
536
- /// returning how many bytes were successfully written.
535
+ /// Implementors of the `Write` trait are sometimes called 'writers'.
537
536
///
538
- /// The `flush` method is useful for adaptors and explicit buffers themselves
539
- /// for ensuring that all buffered data has been pushed out to the "true sink".
537
+ /// Writers are defined by two required methods, `write()` and `flush()`:
540
538
///
541
539
/// * The `write()` method will attempt to write some data into the object,
542
540
/// returning how many bytes were successfully written.
@@ -588,6 +586,20 @@ pub trait Write {
588
586
///
589
587
/// It is **not** considered an error if the entire buffer could not be
590
588
/// written to this writer.
589
+ ///
590
+ /// # Examples
591
+ ///
592
+ /// ```
593
+ /// use std::io::prelude::*;
594
+ /// use std::fs::File;
595
+ ///
596
+ /// # fn foo() -> std::io::Result<()> {
597
+ /// let mut buffer = try!(File::create("foo.txt"));
598
+ ///
599
+ /// try!(buffer.write(b"some bytes"));
600
+ /// # Ok(())
601
+ /// # }
602
+ /// ```
591
603
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
592
604
fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize > ;
593
605
@@ -598,6 +610,22 @@ pub trait Write {
598
610
///
599
611
/// It is considered an error if not all bytes could be written due to
600
612
/// I/O errors or EOF being reached.
613
+ ///
614
+ /// # Examples
615
+ ///
616
+ /// ```
617
+ /// use std::io::prelude::*;
618
+ /// use std::io::BufWriter;
619
+ /// use std::fs::File;
620
+ ///
621
+ /// # fn foo() -> std::io::Result<()> {
622
+ /// let mut buffer = BufWriter::new(try!(File::create("foo.txt")));
623
+ ///
624
+ /// try!(buffer.write(b"some bytes"));
625
+ /// try!(buffer.flush());
626
+ /// # Ok(())
627
+ /// # }
628
+ /// ```
601
629
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
602
630
fn flush ( & mut self ) -> Result < ( ) > ;
603
631
@@ -611,6 +639,20 @@ pub trait Write {
611
639
/// # Errors
612
640
///
613
641
/// This function will return the first error that `write` returns.
642
+ ///
643
+ /// # Examples
644
+ ///
645
+ /// ```
646
+ /// use std::io::prelude::*;
647
+ /// use std::fs::File;
648
+ ///
649
+ /// # fn foo() -> std::io::Result<()> {
650
+ /// let mut buffer = try!(File::create("foo.txt"));
651
+ ///
652
+ /// try!(buffer.write_all(b"some bytes"));
653
+ /// # Ok(())
654
+ /// # }
655
+ /// ```
614
656
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
615
657
fn write_all ( & mut self , mut buf : & [ u8 ] ) -> Result < ( ) > {
616
658
while !buf. is_empty ( ) {
@@ -628,17 +670,41 @@ pub trait Write {
628
670
/// Writes a formatted string into this writer, returning any error
629
671
/// encountered.
630
672
///
631
- /// This method is primarily used to interface with the `format_args!`
632
- /// macro, but it is rare that this should explicitly be called. The
633
- /// `write!` macro should be favored to invoke this method instead.
673
+ /// This method is primarily used to interface with the
674
+ /// [`format_args!`][formatargs] macro, but it is rare that this should
675
+ /// explicitly be called. The [`write!`][write] macro should be favored to
676
+ /// invoke this method instead.
677
+ ///
678
+ /// [formatargs]: ../std/macro.format_args!.html
679
+ /// [write]: ../std/macro.write!.html
634
680
///
635
- /// This function internally uses the `write_all` method on this trait and
636
- /// hence will continuously write data so long as no errors are received.
637
- /// This also means that partial writes are not indicated in this signature.
681
+ /// This function internally uses the [`write_all`][writeall] method on
682
+ /// this trait and hence will continuously write data so long as no errors
683
+ /// are received. This also means that partial writes are not indicated in
684
+ /// this signature.
685
+ ///
686
+ /// [writeall]: #method.write_all
638
687
///
639
688
/// # Errors
640
689
///
641
690
/// This function will return any I/O error reported while formatting.
691
+ ///
692
+ /// # Examples
693
+ ///
694
+ /// ```
695
+ /// use std::io::prelude::*;
696
+ /// use std::fs::File;
697
+ ///
698
+ /// # fn foo() -> std::io::Result<()> {
699
+ /// let mut buffer = try!(File::create("foo.txt"));
700
+ ///
701
+ /// // this call
702
+ /// try!(write!(buffer, "{:.*}", 2, 1.234567));
703
+ /// // turns into this:
704
+ /// try!(buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)));
705
+ /// # Ok(())
706
+ /// # }
707
+ /// ```
642
708
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
643
709
fn write_fmt ( & mut self , fmt : fmt:: Arguments ) -> Result < ( ) > {
644
710
// Create a shim which translates a Write to a fmt::Write and saves
@@ -671,6 +737,23 @@ pub trait Write {
671
737
///
672
738
/// The returned adaptor also implements `Write` and will simply borrow this
673
739
/// current writer.
740
+ ///
741
+ /// # Examples
742
+ ///
743
+ /// ```
744
+ /// use std::io::Write;
745
+ /// use std::fs::File;
746
+ ///
747
+ /// # fn foo() -> std::io::Result<()> {
748
+ /// let mut buffer = try!(File::create("foo.txt"));
749
+ ///
750
+ /// let reference = buffer.by_ref();
751
+ ///
752
+ /// // we can use reference just like our original buffer
753
+ /// try!(reference.write_all(b"some bytes"));
754
+ /// # Ok(())
755
+ /// # }
756
+ /// ```
674
757
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
675
758
fn by_ref ( & mut self ) -> & mut Self where Self : Sized { self }
676
759
@@ -682,6 +765,25 @@ pub trait Write {
682
765
/// implementation do not precisely track where errors happen. For example
683
766
/// an error on the second call to `write` will not report that the first
684
767
/// call to `write` succeeded.
768
+ ///
769
+ /// # Examples
770
+ ///
771
+ /// ```
772
+ /// #![feature(io)]
773
+ /// use std::io::prelude::*;
774
+ /// use std::fs::File;
775
+ ///
776
+ /// # fn foo() -> std::io::Result<()> {
777
+ /// let mut buffer1 = try!(File::create("foo.txt"));
778
+ /// let mut buffer2 = Vec::new();
779
+ ///
780
+ /// // write the output to buffer1 as we read
781
+ /// let mut handle = buffer1.broadcast(&mut buffer2);
782
+ ///
783
+ /// try!(handle.write(b"some bytes"));
784
+ /// # Ok(())
785
+ /// # }
786
+ /// ```
685
787
#[ unstable( feature = "io" , reason = "the semantics of a partial read/write \
686
788
of where errors happen is currently \
687
789
unclear and may change") ]
@@ -706,15 +808,15 @@ pub trait Write {
706
808
///
707
809
/// ```
708
810
/// use std::io;
811
+ /// use std::io::prelude::*;
709
812
/// use std::fs::File;
710
- /// use std::io::Seek;
711
813
/// use std::io::SeekFrom;
712
814
///
713
815
/// # fn foo() -> io::Result<()> {
714
816
/// let mut f = try!(File::open("foo.txt"));
715
817
///
716
818
/// // move the cursor 42 bytes from the start of the file
717
- /// f.seek(SeekFrom::Start(42)).unwrap( );
819
+ /// try!( f.seek(SeekFrom::Start(42)));
718
820
/// # Ok(())
719
821
/// # }
720
822
/// ```
0 commit comments