Skip to content

Commit 1f24882

Browse files
committed
core: Update all Result docs
1 parent 8d63428 commit 1f24882

File tree

1 file changed

+52
-70
lines changed

1 file changed

+52
-70
lines changed

src/libcore/result.rs

+52-70
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,32 @@
8585
//! functions that may encounter errors but don't otherwise return a
8686
//! useful value.
8787
//!
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:
9090
//!
9191
//! ```
92-
//! # #![feature(old_io)]
93-
//! use std::old_io::IoError;
92+
//! use std::io;
9493
//!
9594
//! trait Writer {
96-
//! fn write_line(&mut self, s: &str) -> Result<(), IoError>;
95+
//! fn write_all(&mut self, bytes: &[u8]) -> Result<(), io::Error>;
9796
//! }
9897
//! ```
9998
//!
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>`.*
102101
//!
103102
//! This method doesn't produce a value, but the write may
104103
//! fail. It's crucial to handle the error case, and *not* write
105104
//! something like this:
106105
//!
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::*;
111109
//!
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
114112
//! // value is ignored.
115-
//! file.write_line("important message");
116-
//! drop(file);
113+
//! file.write_all(b"important message");
117114
//! ```
118115
//!
119116
//! If you *do* write that in Rust, the compiler will give you a
@@ -125,37 +122,31 @@
125122
//! a marginally useful message indicating why:
126123
//!
127124
//! ```{.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::*;
131127
//!
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");
135130
//! ```
136131
//!
137132
//! You might also simply assert success:
138133
//!
139134
//! ```{.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());
147139
//! ```
148140
//!
149141
//! Or propagate the error up the call stack with `try!`:
150142
//!
151143
//! ```
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"));
159150
//! Ok(())
160151
//! }
161152
//! ```
@@ -170,48 +161,51 @@
170161
//! It replaces this:
171162
//!
172163
//! ```
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;
176167
//!
177168
//! struct Info {
178169
//! name: String,
179170
//! age: i32,
180171
//! rating: i32,
181172
//! }
182173
//!
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"));
185176
//! // 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()) {
187181
//! return Err(e)
188182
//! }
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()) {
190184
//! return Err(e)
191185
//! }
192-
//! file.write_line(&format!("rating: {}", info.rating))
186+
//! Ok(())
193187
//! }
194188
//! ```
195189
//!
196190
//! With this:
197191
//!
198192
//! ```
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;
202196
//!
203197
//! struct Info {
204198
//! name: String,
205199
//! age: i32,
206200
//! rating: i32,
207201
//! }
208202
//!
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"));
211205
//! // 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()));
215209
//! Ok(())
216210
//! }
217211
//! ```
@@ -464,29 +458,17 @@ impl<T, E> Result<T, E> {
464458
///
465459
/// # Examples
466460
///
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.
469462
///
470463
/// ```
471-
/// # #![feature(old_io)]
472-
/// use std::old_io::*;
464+
/// let line = "1\n2\n3\n4\n";
473465
///
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+
/// }
487471
/// }
488-
///
489-
/// assert!(sum == 10);
490472
/// ```
491473
#[inline]
492474
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)