diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2023-01-13 19:45:26 +0100 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2023-01-13 22:42:11 +0100 |
| commit | a37b4842220a647902a25fcfe68a7d52cda056ad (patch) | |
| tree | 886fad217eca78158b9818c84d86e6ef9833c177 | |
| parent | 222d1ff68d5bfe1dc2d7f3f0c42811fe12964af9 (diff) | |
| download | rust-a37b4842220a647902a25fcfe68a7d52cda056ad.tar.gz rust-a37b4842220a647902a25fcfe68a7d52cda056ad.zip | |
Allow fmt::Arguments::as_str() to return more Some(_).
| -rw-r--r-- | library/core/src/fmt/mod.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 51e6a76cea8..2dacdfd7a02 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -484,9 +484,26 @@ pub struct Arguments<'a> { } impl<'a> Arguments<'a> { - /// Get the formatted string, if it has no arguments to be formatted. + /// Get the formatted string, if it has no arguments to be formatted at runtime. /// - /// This can be used to avoid allocations in the most trivial case. + /// This can be used to avoid allocations in some cases. + /// + /// # Guarantees + /// + /// For `format_args!("just a literal")`, this function is guaranteed to + /// return `Some("just a literal")`. + /// + /// For most cases with placeholders, this function will return `None`. + /// + /// However, the compiler may perform optimizations that can cause this + /// function to return `Some(_)` even if the format string contains + /// placeholders. For example, `format_args!("Hello, {}!", "world")` may be + /// optimized to `format_args!("Hello, world!")`, such that `as_str()` + /// returns `Some("Hello, world!")`. + /// + /// The behavior for anything but the trivial case (without placeholders) + /// is not guaranteed, and should not be relied upon for anything other + /// than optimization. /// /// # Examples /// @@ -507,7 +524,7 @@ impl<'a> Arguments<'a> { /// ```rust /// assert_eq!(format_args!("hello").as_str(), Some("hello")); /// assert_eq!(format_args!("").as_str(), Some("")); - /// assert_eq!(format_args!("{}", 1).as_str(), None); + /// assert_eq!(format_args!("{:?}", std::env::current_dir()).as_str(), None); /// ``` #[stable(feature = "fmt_as_str", since = "1.52.0")] #[rustc_const_unstable(feature = "const_arguments_as_str", issue = "103900")] |
