Crates.io | fstrings-rust |
lib.rs | fstrings-rust |
version | 1.0.1 |
source | src |
created_at | 2021-04-16 05:40:34.511803 |
updated_at | 2021-04-16 06:01:20.893646 |
description | Python3 fstring interpolation in Rust |
homepage | https://crates.io/crates/fstrings-rust |
repository | https://github.com/fx-kirin/fstrings-rust |
max_upload_size | |
id | 385207 |
size | 12,345 |
::fstrings
The interpolation works as follows:
if the (template) string literal contains a named parameter
(e.g. {name}
)
and no name = value
argument is fed to the formatting call,
then an automatic name = name
argument is added, so that the variable is
effectively interpolated from the current scope.
#[macro_use]
extern crate fstrings;
fn main ()
{
let name = "World";
// Usage is simple: just append `_f` to the name of any formatting macro
println_f!("Hello, {name}!");
assert_eq!(
f!("Hello, {name}!"), // shorthand for String creation (Python-like)
String::from("Hello, World!"),
);
// ## Advanced cases:
{
// It remains compatible with classic formatting parameters
assert_eq!(
f!("{hi}, {name}!", hi = "Hello"),
"Hello, World!",
);
// You can override / shadow the named arguments
assert_eq!(
f!("Hello, {name}!", name = "Earth"),
"Hello, Earth!",
);
// You can use field access (but no method calls!)
let foo = Foo { name }; /* where */ struct Foo<T> { name: T }
assert_eq!(
f!("Hello, {foo.name}!"),
"Hello, World!",
);
// This also works with tuple indexing.
let ft_and_name = (42, name);
assert_eq!(
f!("Hello, {ft_and_name.1}!"),
"Hello, World!",
);
// You can use fstrings to debug by appending a `=` after the
// interpolated expression.
let x = 0b_101010;
assert_eq!(
f!("In this context {x=}"),
"In this context x = 42",
);
}
}