pub struct FormatString { /* private fields */ }Expand description
A parsed format string that can be used for dynamic formatting.
FormatString represents a string with embedded format specifications that has been
parsed and can be used with the dformat! macro to perform dynamic formatting operations.
It contains the original string along with information about the format specifications
found within it.
§Examples
Basic usage:
use dyf::FormatString;
let fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();
assert!(fmt.contains_format());Creating and using a format string:
use dyf::{FormatString, dformat};
let fmt = FormatString::from_string("{:>10} {:.2}".to_string()).unwrap();
let result = dformat!(&fmt, 42, 3.14159).unwrap();
assert_eq!(result, " 42 3.14");Converting between string types:
use dyf::FormatString;
let fmt = FormatString::from_string("Value: {:05}".to_string()).unwrap();
let fmt_str = fmt.to_string_lossy();
let owned_str = fmt.into_string();Implementations§
Source§impl FormatString
impl FormatString
Sourcepub fn from_string(s: String) -> Result<Self, Error>
pub fn from_string(s: String) -> Result<Self, Error>
Creates a new FormatString from a string.
§Arguments
s- The string containing format specifications
§Returns
A Result containing the parsed FormatString or an error if parsing fails.
§Errors
This function may return an error if the input string contains invalid format specifications that cannot be parsed.
§Examples
use dyf::FormatString;
let fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Converts the FormatString into its inner string.
This consumes the FormatString and returns the original string that was used
to create it.
§Examples
use dyf::FormatString;
let fmt = FormatString::from_string("Test: {}".to_string()).unwrap();
let s = fmt.into_string();
assert_eq!(s, "Test: {}");Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Returns a borrowed version of the format string.
This provides access to the original string without consuming the FormatString.
§Examples
use dyf::FormatString;
let fmt = FormatString::from_string("Value: {:.2}".to_string()).unwrap();
let borrowed = fmt.to_string_lossy();
assert_eq!(&*borrowed, "Value: {:.2}");Sourcepub fn contains_format(&self) -> bool
pub fn contains_format(&self) -> bool
Returns true if the format string contains any format specifications.
§Examples
use dyf::FormatString;
let with_fmt = FormatString::from_string("Hello, {}!".to_string()).unwrap();
assert!(with_fmt.contains_format());
let without_fmt = FormatString::from_string("Hello, world!".to_string()).unwrap();
assert!(!without_fmt.contains_format());Trait Implementations§
Source§impl Clone for FormatString
impl Clone for FormatString
Source§fn clone(&self) -> FormatString
fn clone(&self) -> FormatString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more