macro_rules! py_format {
($py: expr, $($arg:tt)*) => { ... };
}Expand description
This macro is analogous to Rust’s format! macro, but returns a PyString instead of a String.
§Arguments
The arguments are exactly like format!, but with py (a Python token) as the first argument:
§Interning Advantage
If the format string is a static string and all arguments are constant at compile time,
this macro will intern the string in Python, offering better performance and memory usage
compared to PyString::from_fmt.
Python::attach(|py| {
let py_string: Bound<'_, PyString> = py_format!(py, "{} {}", "hello", "world").unwrap();
assert_eq!(py_string.to_string(), "hello world");
});