Skip to content

Commit aa55f9a

Browse files
Rollup merge of rust-lang#35862 - Stebalien:fmt-docs, r=steveklabnik
Clarify/fix formatting docs concerning fmt::Result/fmt::Error 1. `fmt::Result` != `io::Result<()>` 2. Formatters should only propagate errors, not return their own. Confusion on reddit: https://www.reddit.com/r/rust/comments/4yorxr/is_implt_tostring_for_t_where_t_display_sized_a/
2 parents 5ab0809 + c7d5f7e commit aa55f9a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/libcollections/fmt.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,15 @@
165165
//! provides some helper methods.
166166
//!
167167
//! Additionally, the return value of this function is `fmt::Result` which is a
168-
//! typedef to `Result<(), std::io::Error>` (also known as `std::io::Result<()>`).
169-
//! Formatting implementations should ensure that they return errors from `write!`
170-
//! correctly (propagating errors upward).
168+
//! type alias of `Result<(), std::fmt::Error>`. Formatting implementations
169+
//! should ensure that they propagate errors from the `Formatter` (e.g., when
170+
//! calling `write!`) however, they should never return errors spuriously. That
171+
//! is, a formatting implementation must and may only return an error if the
172+
//! passed-in `Formatter` returns an error. This is because, contrary to what
173+
//! the function signature might suggest, string formatting is an infallible
174+
//! operation. This function only returns a result because writing to the
175+
//! underlying stream might fail and it must provide a way to propagate the fact
176+
//! that an error has occurred back up the stack.
171177
//!
172178
//! An example of implementing the formatting traits would look
173179
//! like:

0 commit comments

Comments
 (0)