- Start Date: 2014-12-19
- RFC PR: rust-lang/rfcs#504
- Rust Issue: rust-lang/rust#20013
Summary
Today's Show
trait will be tasked with the purpose of providingString
, will be introduced to the std::fmt
module to in order
The String
trait will take{}
format specifier and the Show
trait will move to the now-open {:?}
specifier.
Motivation
The formatting traits today largely provide clear guidance to what they are intended for. For example the Binary
trait is intended for printing the binaryShow
trait, however, is not quite well defined
One of the use cases of Show
today is to provide a "debugging view" of a type. This providesShow
trait, however, is also used for printing user-facing information. This flavor of usage is intended for display to all users as opposedShow
trait is connected to the ToString
trait providingto_string
method unconditionally.
From these use cases of Show
, a number of pain points have arisen over time:
- It's not clear whether all types should implement実装する
Show
or not. Types likePath
quite intentionally avoid避ける、回避するexposing a string representation表現(due to paths not being valid有効な、正しいUTF-8 always) and hence do not want ato_string
method to be defined定義するon them. - It is quite common to use
#[deriving(Show)]
to easily print a Rust structure. This is not possible, however, when particular members do not implement実装するShow
(for example aPath
). - Some types, such as a
String
, desire the ability to "inspect" the representation表現as well as printing the representation.表現An inspection mode, for example, would escape characters文字like newlines.改行 - Common pieces of functionality, such as
assert_eq!
are tied to theShow
trait which is not necessarily implemented実装するfor all types.
The purpose of this RFC is to clearly defineShow
trait is intended to be used for, as well as providing
Detailed Design設計(する)
As describedShow
trait are actually motivations for two separate formatting traits. One trait will be intended for all Rust types to implementassert_eq!
or generalprintln!
statements.
This RFC proposes naming these two traits Show
and String
, respectively.
The String
trait
A new formatting trait will be addedstd::fmt
as follows:
This trait is identicalString
trait will be used with the {}
format specifier, typically
An implementationString
trait is an assertion
If the type cannot necessarily be reconstructed from a string, then the output may be less descriptive than the type can provide, but it is guaranteed
It is not expected that all types implementString
trait. Not all types can satisfy the purpose of this trait, and for example the following types will not implementString
trait:
Path
will abstain as it is not guaranteed保証するto contain含むvalid有効な、正しいUTF-8 data.CString
will abstain for the same reasons asPath
.RefCell
will abstain as it may not be accessed at all times to be represented表現するas aString
.Weak
references参照will abstain for the same reasons asRefCell
.
Almost all types that implementShow
in the standard library today, however, will implementString
trait. For example all primitive integerString
trait. The output format will not change from what it is today (no extra escaping or debugging will occur).
The compiler will not provide an implementation#[deriving(String)]
for types.
The Show
trait
The current Show
trait will not change location nor definition,{:?}
specifier instead of the {}
specifier (which String
now uses).
An implementationShow
trait is expected for all types in Rust and providesShow
should never be used to reconstruct the object itself as it is not guaranteed
The purpose of the Show
trait is to facilitate debugging Rust code which impliesShow
will gain an implementationShow
trait including Path
, RefCell
, and Weak
references.
Many implementationsShow
in the standard library will differ from what they currently are today. For example str
's implementation
ImplementationsShow
trait are expected to never panic!
and always produce#[deriving(Show)]
implementation
The ToString
trait
Today the ToString
trait is connected to the Show
trait, but this RFC proposes wiring it to the newly-proposed String
trait instead. This switch enables users of to_string
to rely on the same guaranteesString
as well as not erroneouslyto_string
method on types that are not intended to have one.
It is strongly discouraged to provide an implementationToString
trait and not the String
trait.
Drawbacks
It is inherently easier to understand fewer concepts from the standard library and introducing multiple
This RFC establishes a convention that Show
and String
produceTextWriter
which we are currently not willing to stabilize for the 1.0 release.
The default format specifier, {}
, will quickly become unable to print many types in Rust. Without a #[deriving]
implementation,{:?}
for inspecting Rust types, providingString
to include escaped charactersShow
, it is seen as a non-starter to make it the "default" via {}
.
It may be too ambitious to defineString
is a non-lossy representation
Alternatives
The names String
and Show
may not necessarily imply "user readable" and "debuggable". An alternativeShow
for user readabilityInspect
for debugging. This alternativeRepr
. This RFC, however, has chosen String
for user readabilityToString
trait as well as emphasizing that the type can be faithfully representedString
. Additionally, this RFC considersShow
roughly on par with other alternatives and would help reduce churn for code migrating today.
Unresolved Questions
None at this time.