Skip to content

Commit 846763a

Browse files
authored
Unrolled build for #136687
Rollup merge of #136687 - joshtriplett:improve-display-and-fromstr-docs, r=Amanieu Improve the documentation of `Display` and `FromStr`, and their interactions In particular: - `Display` is not necessarily lossless - The output of `Display` might not be parseable by `FromStr`, and might not produce the same value if it is. - Calling `.parse()` on the output of `Display` is usually a mistake unless a type's documented output and input formats match. - The input formats accepted by `FromStr` depend on the type. This documentation adds no API surface area and makes no guarantees about stability. To the best of my knowledge, everything it says is already established to be true. As such, I don't think it needs an FCP.
2 parents d9a7393 + 742014e commit 846763a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

library/core/src/fmt/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,20 @@ pub use macros::Debug;
928928
/// [tostring]: ../../std/string/trait.ToString.html
929929
/// [tostring_function]: ../../std/string/trait.ToString.html#tymethod.to_string
930930
///
931+
/// # Completeness and parseability
932+
///
933+
/// `Display` for a type might not necessarily be a lossless or complete representation of the type.
934+
/// It may omit internal state, precision, or other information the type does not consider important
935+
/// for user-facing output, as determined by the type. As such, the output of `Display` might not be
936+
/// possible to parse, and even if it is, the result of parsing might not exactly match the original
937+
/// value.
938+
///
939+
/// However, if a type has a lossless `Display` implementation whose output is meant to be
940+
/// conveniently machine-parseable and not just meant for human consumption, then the type may wish
941+
/// to accept the same format in `FromStr`, and document that usage. Having both `Display` and
942+
/// `FromStr` implementations where the result of `Display` cannot be parsed with `FromStr` may
943+
/// surprise users.
944+
///
931945
/// # Internationalization
932946
///
933947
/// Because a type can only have one `Display` implementation, it is often preferable

library/core/src/str/traits.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,20 @@ unsafe impl SliceIndex<str> for ops::RangeToInclusive<usize> {
756756
/// parse an `i32` with `FromStr`, but not a `&i32`. You can parse a struct that
757757
/// contains an `i32`, but not one that contains an `&i32`.
758758
///
759+
/// # Input format and round-tripping
760+
///
761+
/// The input format expected by a type's `FromStr` implementation depends on the type. Check the
762+
/// type's documentation for the input formats it knows how to parse. Note that the input format of
763+
/// a type's `FromStr` implementation might not necessarily accept the output format of its
764+
/// `Display` implementation, and even if it does, the `Display` implementation may not be lossless
765+
/// so the round-trip may lose information.
766+
///
767+
/// However, if a type has a lossless `Display` implementation whose output is meant to be
768+
/// conveniently machine-parseable and not just meant for human consumption, then the type may wish
769+
/// to accept the same format in `FromStr`, and document that usage. Having both `Display` and
770+
/// `FromStr` implementations where the result of `Display` cannot be parsed with `FromStr` may
771+
/// surprise users.
772+
///
759773
/// # Examples
760774
///
761775
/// Basic implementation of `FromStr` on an example `Point` type:

0 commit comments

Comments
 (0)