Skip to content

Commit f2655e2

Browse files
committed
Note that formatters should not return spurious errors.
Doing otherwise would break traits like `ToString`.
1 parent e4dd785 commit f2655e2

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/libcollections/fmt.rs

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

0 commit comments

Comments
 (0)