|
85 | 85 | //! functions that may encounter errors but don't otherwise return a
|
86 | 86 | //! useful value.
|
87 | 87 | //!
|
88 |
| -//! Consider the `write_line` method defined for I/O types |
89 |
| -//! by the [`Writer`](../old_io/trait.Writer.html) trait: |
| 88 | +//! Consider the `write_all` method defined for I/O types |
| 89 | +//! by the [`Write`](../io/trait.Write.html) trait: |
90 | 90 | //!
|
91 | 91 | //! ```
|
92 |
| -//! # #![feature(old_io)] |
93 |
| -//! use std::old_io::IoError; |
| 92 | +//! use std::io; |
94 | 93 | //!
|
95 | 94 | //! trait Writer {
|
96 |
| -//! fn write_line(&mut self, s: &str) -> Result<(), IoError>; |
| 95 | +//! fn write_all(&mut self, bytes: &[u8]) -> Result<(), io::Error>; |
97 | 96 | //! }
|
98 | 97 | //! ```
|
99 | 98 | //!
|
100 |
| -//! *Note: The actual definition of `Writer` uses `IoResult`, which |
101 |
| -//! is just a synonym for `Result<T, IoError>`.* |
| 99 | +//! *Note: The actual definition of `Write` uses `io::Result`, which |
| 100 | +//! is just a synonym for `Result<T, io::Error>`.* |
102 | 101 | //!
|
103 | 102 | //! This method doesn't produce a value, but the write may
|
104 | 103 | //! fail. It's crucial to handle the error case, and *not* write
|
105 | 104 | //! something like this:
|
106 | 105 | //!
|
107 |
| -//! ```{.ignore} |
108 |
| -//! # #![feature(old_io)] |
109 |
| -//! use std::old_io::*; |
110 |
| -//! use std::old_path::Path; |
| 106 | +//! ```no_run |
| 107 | +//! use std::fs::File; |
| 108 | +//! use std::io::prelude::*; |
111 | 109 | //!
|
112 |
| -//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write); |
113 |
| -//! // If `write_line` errors, then we'll never know, because the return |
| 110 | +//! let mut file = File::create("valuable_data.txt").unwrap(); |
| 111 | +//! // If `write_all` errors, then we'll never know, because the return |
114 | 112 | //! // value is ignored.
|
115 |
| -//! file.write_line("important message"); |
116 |
| -//! drop(file); |
| 113 | +//! file.write_all(b"important message"); |
117 | 114 | //! ```
|
118 | 115 | //!
|
119 | 116 | //! If you *do* write that in Rust, the compiler will give you a
|
|
125 | 122 | //! a marginally useful message indicating why:
|
126 | 123 | //!
|
127 | 124 | //! ```{.no_run}
|
128 |
| -//! # #![feature(old_io, old_path)] |
129 |
| -//! use std::old_io::*; |
130 |
| -//! use std::old_path::Path; |
| 125 | +//! use std::fs::File; |
| 126 | +//! use std::io::prelude::*; |
131 | 127 | //!
|
132 |
| -//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write); |
133 |
| -//! file.write_line("important message").ok().expect("failed to write message"); |
134 |
| -//! drop(file); |
| 128 | +//! let mut file = File::create("valuable_data.txt").unwrap(); |
| 129 | +//! file.write_all(b"important message").ok().expect("failed to write message"); |
135 | 130 | //! ```
|
136 | 131 | //!
|
137 | 132 | //! You might also simply assert success:
|
138 | 133 | //!
|
139 | 134 | //! ```{.no_run}
|
140 |
| -//! # #![feature(old_io, old_path)] |
141 |
| -//! # use std::old_io::*; |
142 |
| -//! # use std::old_path::Path; |
143 |
| -//! |
144 |
| -//! # let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write); |
145 |
| -//! assert!(file.write_line("important message").is_ok()); |
146 |
| -//! # drop(file); |
| 135 | +//! # use std::fs::File; |
| 136 | +//! # use std::io::prelude::*; |
| 137 | +//! # let mut file = File::create("valuable_data.txt").unwrap(); |
| 138 | +//! assert!(file.write_all(b"important message").is_ok()); |
147 | 139 | //! ```
|
148 | 140 | //!
|
149 | 141 | //! Or propagate the error up the call stack with `try!`:
|
150 | 142 | //!
|
151 | 143 | //! ```
|
152 |
| -//! # #![feature(old_io, old_path)] |
153 |
| -//! # use std::old_io::*; |
154 |
| -//! # use std::old_path::Path; |
155 |
| -//! fn write_message() -> Result<(), IoError> { |
156 |
| -//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write); |
157 |
| -//! try!(file.write_line("important message")); |
158 |
| -//! drop(file); |
| 144 | +//! # use std::fs::File; |
| 145 | +//! # use std::io::prelude::*; |
| 146 | +//! # use std::io; |
| 147 | +//! fn write_message() -> io::Result<()> { |
| 148 | +//! let mut file = try!(File::create("valuable_data.txt")); |
| 149 | +//! try!(file.write_all(b"important message")); |
159 | 150 | //! Ok(())
|
160 | 151 | //! }
|
161 | 152 | //! ```
|
|
170 | 161 | //! It replaces this:
|
171 | 162 | //!
|
172 | 163 | //! ```
|
173 |
| -//! # #![feature(old_io, old_path)] |
174 |
| -//! use std::old_io::*; |
175 |
| -//! use std::old_path::Path; |
| 164 | +//! use std::fs::File; |
| 165 | +//! use std::io::prelude::*; |
| 166 | +//! use std::io; |
176 | 167 | //!
|
177 | 168 | //! struct Info {
|
178 | 169 | //! name: String,
|
179 | 170 | //! age: i32,
|
180 | 171 | //! rating: i32,
|
181 | 172 | //! }
|
182 | 173 | //!
|
183 |
| -//! fn write_info(info: &Info) -> Result<(), IoError> { |
184 |
| -//! let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write); |
| 174 | +//! fn write_info(info: &Info) -> io::Result<()> { |
| 175 | +//! let mut file = try!(File::create("my_best_friends.txt")); |
185 | 176 | //! // Early return on error
|
186 |
| -//! if let Err(e) = file.write_line(&format!("name: {}", info.name)) { |
| 177 | +//! if let Err(e) = file.write_all(format!("name: {}\n", info.name).as_bytes()) { |
| 178 | +//! return Err(e) |
| 179 | +//! } |
| 180 | +//! if let Err(e) = file.write_all(format!("age: {}\n", info.age).as_bytes()) { |
187 | 181 | //! return Err(e)
|
188 | 182 | //! }
|
189 |
| -//! if let Err(e) = file.write_line(&format!("age: {}", info.age)) { |
| 183 | +//! if let Err(e) = file.write_all(format!("rating: {}\n", info.rating).as_bytes()) { |
190 | 184 | //! return Err(e)
|
191 | 185 | //! }
|
192 |
| -//! file.write_line(&format!("rating: {}", info.rating)) |
| 186 | +//! Ok(()) |
193 | 187 | //! }
|
194 | 188 | //! ```
|
195 | 189 | //!
|
196 | 190 | //! With this:
|
197 | 191 | //!
|
198 | 192 | //! ```
|
199 |
| -//! # #![feature(old_io, old_path)] |
200 |
| -//! use std::old_io::*; |
201 |
| -//! use std::old_path::Path; |
| 193 | +//! use std::fs::File; |
| 194 | +//! use std::io::prelude::*; |
| 195 | +//! use std::io; |
202 | 196 | //!
|
203 | 197 | //! struct Info {
|
204 | 198 | //! name: String,
|
205 | 199 | //! age: i32,
|
206 | 200 | //! rating: i32,
|
207 | 201 | //! }
|
208 | 202 | //!
|
209 |
| -//! fn write_info(info: &Info) -> Result<(), IoError> { |
210 |
| -//! let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write); |
| 203 | +//! fn write_info(info: &Info) -> io::Result<()> { |
| 204 | +//! let mut file = try!(File::create("my_best_friends.txt")); |
211 | 205 | //! // Early return on error
|
212 |
| -//! try!(file.write_line(&format!("name: {}", info.name))); |
213 |
| -//! try!(file.write_line(&format!("age: {}", info.age))); |
214 |
| -//! try!(file.write_line(&format!("rating: {}", info.rating))); |
| 206 | +//! try!(file.write_all(format!("name: {}\n", info.name).as_bytes())); |
| 207 | +//! try!(file.write_all(format!("age: {}\n", info.age).as_bytes())); |
| 208 | +//! try!(file.write_all(format!("rating: {}\n", info.rating).as_bytes())); |
215 | 209 | //! Ok(())
|
216 | 210 | //! }
|
217 | 211 | //! ```
|
@@ -464,29 +458,17 @@ impl<T, E> Result<T, E> {
|
464 | 458 | ///
|
465 | 459 | /// # Examples
|
466 | 460 | ///
|
467 |
| - /// Sum the lines of a buffer by mapping strings to numbers, |
468 |
| - /// ignoring I/O and parse errors: |
| 461 | + /// Print the numbers on each line of a string multiplied by two. |
469 | 462 | ///
|
470 | 463 | /// ```
|
471 |
| - /// # #![feature(old_io)] |
472 |
| - /// use std::old_io::*; |
| 464 | + /// let line = "1\n2\n3\n4\n"; |
473 | 465 | ///
|
474 |
| - /// let mut buffer: &[u8] = b"1\n2\n3\n4\n"; |
475 |
| - /// let mut buffer = &mut buffer; |
476 |
| - /// |
477 |
| - /// let mut sum = 0; |
478 |
| - /// |
479 |
| - /// while !buffer.is_empty() { |
480 |
| - /// let line: IoResult<String> = buffer.read_line(); |
481 |
| - /// // Convert the string line to a number using `map` and `from_str` |
482 |
| - /// let val: IoResult<i32> = line.map(|line| { |
483 |
| - /// line.trim_right().parse::<i32>().unwrap_or(0) |
484 |
| - /// }); |
485 |
| - /// // Add the value if there were no errors, otherwise add 0 |
486 |
| - /// sum += val.unwrap_or(0); |
| 466 | + /// for num in line.lines() { |
| 467 | + /// match num.parse::<i32>().map(|i| i * 2) { |
| 468 | + /// Ok(n) => println!("{}", n), |
| 469 | + /// Err(..) => {} |
| 470 | + /// } |
487 | 471 | /// }
|
488 |
| - /// |
489 |
| - /// assert!(sum == 10); |
490 | 472 | /// ```
|
491 | 473 | #[inline]
|
492 | 474 | #[stable(feature = "rust1", since = "1.0.0")]
|
|
0 commit comments