Skip to content

Commit e17c17a

Browse files
committed
Make Arguments::as_str() work for empty format strings.
1 parent bc4e33e commit e17c17a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libcore/fmt/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,17 @@ impl<'a> Arguments<'a> {
436436
/// #![feature(fmt_as_str)]
437437
///
438438
/// assert_eq!(format_args!("hello").as_str(), Some("hello"));
439+
/// assert_eq!(format_args!("").as_str(), Some(""));
439440
/// assert_eq!(format_args!("{}", 1).as_str(), None);
440441
/// ```
441442
#[unstable(feature = "fmt_as_str", issue = "none")]
442443
#[inline]
443444
pub fn as_str(&self) -> Option<&'a str> {
444-
if self.args.is_empty() && self.pieces.len() == 1 { Some(self.pieces[0]) } else { None }
445+
match (self.pieces, self.args) {
446+
([], []) => Some(""),
447+
([s], []) => Some(s),
448+
_ => None,
449+
}
445450
}
446451
}
447452

0 commit comments

Comments
 (0)