Skip to content

Commit 1b9fdbf

Browse files
authored
Rollup merge of rust-lang#59596 - LukasKalbertodt:fix-range-fmt, r=Kimundi
Forward formatter settings to bounds of `Range<T>` in `fmt::Debug` impl Before this change, formatter settings were lost when printing a `Range`. For example, printing a `Range<f32>` with `{:.2?}` would not apply the precision modifier when printing the floats. Now the `Debug` impls look a bit more verbose, but modifier are not lost. --- I assume the exact output of `Debug` impls in `std` cannot be relied on by users and thus can change, right?
2 parents b3a6f8f + d3d3049 commit 1b9fdbf

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

Diff for: src/libcore/ops/range.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ pub struct Range<Idx> {
8585
#[stable(feature = "rust1", since = "1.0.0")]
8686
impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
8787
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
88-
write!(fmt, "{:?}..{:?}", self.start, self.end)
88+
self.start.fmt(fmt)?;
89+
write!(fmt, "..")?;
90+
self.end.fmt(fmt)?;
91+
Ok(())
8992
}
9093
}
9194

@@ -184,7 +187,9 @@ pub struct RangeFrom<Idx> {
184187
#[stable(feature = "rust1", since = "1.0.0")]
185188
impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
186189
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
187-
write!(fmt, "{:?}..", self.start)
190+
self.start.fmt(fmt)?;
191+
write!(fmt, "..")?;
192+
Ok(())
188193
}
189194
}
190195

@@ -266,7 +271,9 @@ pub struct RangeTo<Idx> {
266271
#[stable(feature = "rust1", since = "1.0.0")]
267272
impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
268273
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
269-
write!(fmt, "..{:?}", self.end)
274+
write!(fmt, "..")?;
275+
self.end.fmt(fmt)?;
276+
Ok(())
270277
}
271278
}
272279

@@ -467,7 +474,10 @@ impl<Idx> RangeInclusive<Idx> {
467474
#[stable(feature = "inclusive_range", since = "1.26.0")]
468475
impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
469476
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
470-
write!(fmt, "{:?}..={:?}", self.start, self.end)
477+
self.start.fmt(fmt)?;
478+
write!(fmt, "..=")?;
479+
self.end.fmt(fmt)?;
480+
Ok(())
471481
}
472482
}
473483

@@ -602,7 +612,9 @@ pub struct RangeToInclusive<Idx> {
602612
#[stable(feature = "inclusive_range", since = "1.26.0")]
603613
impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
604614
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
605-
write!(fmt, "..={:?}", self.end)
615+
write!(fmt, "..=")?;
616+
self.end.fmt(fmt)?;
617+
Ok(())
606618
}
607619
}
608620

0 commit comments

Comments
 (0)