gen_static_message!() { /* proc-macro */ }Expand description
Generates a static message with placeholders for arguments.
This macro creates a StaticMessage that can be used to format strings with
a fixed number of arguments. The placeholders in the message are denoted by
{0}, {1}, etc., which correspond to the arguments provided during formatting.
§Notes
- The number of placeholders in the message must match the number of arguments
specified in the
StaticMessagetype. - The macro supports using constants within the message string.
- You can include numeric constants directly in the message using the
{u:}or{i:}syntax for unsigned and signed integers, respectively.
§Examples
use local_fmt::{gen_static_message, StaticMessage};
// Example with argument
{
const MESSAGE: StaticMessage<1> = gen_static_message!("Hello! {0}");
let text = MESSAGE.format(&["World!"]);
assert_eq!(text, "Hello! World!");
}
// Example with const placeholder
{
const HELLO: &str = "Hello";
const MESSAGE: StaticMessage<2> = gen_static_message!("{HELLO} {0} World! {1}");
let text = MESSAGE.format(&["Beautiful", "Rust!"]);
assert_eq!(text, "Hello Beautiful World! Rust!");
}
// Example with duplicate arguments
{
const MESSAGE: StaticMessage<1> = gen_static_message!("{0} World! {0}");
let text = MESSAGE.format(&["Beautiful"]);
assert_eq!(text, "Beautiful World! Beautiful");
}
// Example with unsigned number
{
const NUM: usize = 123456789;
const MESSAGE: StaticMessage<1> = gen_static_message!("Hello! {0} {u:NUM}");
let text = MESSAGE.format(&["World!"]);
assert_eq!(text, "Hello! World! 123456789");
}
// Example with signed number
{
const NUM: i32 = -123456789;
const MESSAGE: StaticMessage<1> = gen_static_message!("Hello! {0} {i:NUM}");
let text = MESSAGE.format(&["World!"]);
assert_eq!(text, "Hello! World! -123456789");
}