A macro for formatting non literal strings at runtime in Rust.
This library aims for formatting strings and numbers rather than an generic type. Syntax for the format string is derived from std::fmt.
Add this to your Cargo.toml file.
toml
[dependencies]
formatx = "0.1"
Or add from command line.
bash
$ cargo add formatx@0.1
See docs and examples to know how to use it.
SOURCE: format! with non literal string
```rust use formatx::formatx;
fn message(language: &str, name: &str, number: i32) -> String { let s = match language { "french" => "Bonjour {}, le nombre est {}", "spanish" => "Hola {}, el numero es {}", _ => "Hi {}, the number is {}", }; formatx!(s, name, number).unwrap() }
fn main() { println!("{}", message("french", "Léa", 1)); println!("{}", message("spanish", "Sofia", 2)); println!("{}", message("english", "Ashley", 3)); } ```
OUTPUT
Bonjour Léa, le nombre est 1
Hola Sofia, el numero es 2
Hi Ashley, the number is 3
Examples given below will always panic.
Only types which implements Display + Debug traits are supported. Other formatting-traits aren't supported.
Local variable interpolation isn't supported.
rust
let people = "Rustaceans";
formatx!("Hello {people}!").unwrap().text().unwrap();
rust
formatx!("{1} {} {0} {}", 1, 2).unwrap();
$
sign argument isn't supported.rust
formatx!("{:width$}!", "x", width = 5).unwrap();
.*
can't be used to set precision.rust
formatx!("{:.*}", 5, 0.01).unwrap();
Dual Licensed