Macro format_ident
macro_rules! format_ident {
($fmt:expr) => { ... };
($fmt:expr, $($rest:tt)*) => { ... };
}
Formatting macro for constructing Idents.
Syntax
Syntax is copied from the [format!] macro, supporting both positional and
named arguments.
Only a limited set of formatting traits are supported. The current mapping of format types to traits is:
{}⇒IdentFragment{:o}⇒Octal{:x}⇒LowerHex{:X}⇒UpperHex{:b}⇒Binary
See std::fmt for more information.
IdentFragment
Unlike format!, this macro uses the IdentFragment formatting trait by
default. This trait is like Display, with a few differences:
IdentFragmentis only implemented for a limited set of types, such as unsigned integers and strings.Identarguments will have theirr#prefixes stripped, if present.
Hygiene
The Span of the first Ident argument is used as the span of the final
identifier, falling back to Span::call_site when no identifiers are
provided.
# use format_ident;
# let ident = format_ident!;
// If `ident` is an Ident, the span of `my_ident` will be inherited from it.
let my_ident = format_ident!;
assert_eq!;
Alternatively, the span can be overridden by passing the span named
argument.
# use format_ident;
# const IGNORE_TOKENS: &'static str = stringify! ;
# let my_span = call_site;
format_ident!;
Panics
This method will panic if the resulting formatted string is not a valid identifier.
Examples
Composing raw and non-raw identifiers:
# use format_ident;
let my_ident = format_ident!;
assert_eq!;
let raw = format_ident!;
assert_eq!;
let my_ident_raw = format_ident!;
assert_eq!;
Integer formatting options:
# use format_ident;
let num: u32 = 10;
let decimal = format_ident!;
assert_eq!;
let octal = format_ident!;
assert_eq!;
let binary = format_ident!;
assert_eq!;
let lower_hex = format_ident!;
assert_eq!;
let upper_hex = format_ident!;
assert_eq!;