2025-08-20 14:48:48 +00:00
<!DOCTYPE html> < html lang = "en" >< head >< meta charset = "utf-8" >< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >< meta name = "generator" content = "rustdoc" >< meta name = "description" content = "Formatting macro for constructing `Ident`s." >< title > format_ident in quote - Rust</ title >< script > if ( window . location . protocol !== "file:" ) document . head . insertAdjacentHTML ( "beforeend" , "SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2" . split ( "," ). map ( f => `<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/ ${ f } ">` ). join ( "" ))</ script >< link rel = "stylesheet" href = "../static.files/normalize-9960930a.css" >< link rel = "stylesheet" href = "../static.files/rustdoc-84e720fa.css" >< meta name = "rustdoc-vars" data-root-path = "../" data-static-root-path = "../static.files/" data-current-crate = "quote" data-themes = "" data-resource-suffix = "" data-rustdoc-version = "1.89.0 (29483883e 2025-08-04)" data-channel = "1.89.0" data-search-js = "search-92309212.js" data-settings-js = "settings-5514c975.js" >< script src = "../static.files/storage-4e99c027.js" ></ script >< script defer src = "sidebar-items.js" ></ script >< script defer src = "../static.files/main-fd3af306.js" ></ script >< noscript >< link rel = "stylesheet" href = "../static.files/noscript-32bb7600.css" ></ noscript >< link rel = "alternate icon" type = "image/png" href = "../static.files/favicon-32x32-6580c154.png" >< link rel = "icon" type = "image/svg+xml" href = "../static.files/favicon-044be391.svg" ></ head >< body class = "rustdoc macro" > <!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--> < nav class = "mobile-topbar" >< button class = "sidebar-menu-toggle" title = "show sidebar" ></ button ></ nav >< nav class = "sidebar" >< div class = "sidebar-crate" >< h2 >< a href = "../quote/index.html" > quote</ a >< span class = "version" > 1.0.40</ span ></ h2 ></ div >< div class = "sidebar-elems" >< section id = "rustdoc-toc" >< h2 class = "location" >< a href = "#" > format_< wbr > ident</ a ></ h2 >< h3 >< a href = "#" > Sections</ a ></ h3 >< ul class = "block top-toc" >< li >< a href = "#syntax" title = "Syntax" > Syntax</ a ></ li >< li >< a href = "#identfragment" title = "IdentFragment" > IdentFragment</ a ></ li >< li >< a href = "#hygiene" title = "Hygiene" > Hygiene</ a ></ li >< li >< a href = "#panics" title = "Panics" > Panics</ a ></ li >< li >< a href = "#examples" title = "Examples" > Examples</ a ></ li ></ ul ></ section >< div id = "rustdoc-modnav" >< h2 class = "in-crate" >< a href = "index.html" > In crate quote</ a ></ h2 ></ div ></ div ></ nav >< div class = "sidebar-resizer" title = "Drag to resize sidebar" ></ div >< main >< div class = "width-limiter" >< rustdoc-search ></ rustdoc-search >< section id = "main-content" class = "content" >< div class = "main-heading" >< div class = "rustdoc-breadcrumbs" >< a href = "index.html" > quote</ a ></ div >< h1 > Macro < span class = "macro" > format_ident</ span >< button id = "copy-path" title = "Copy item path to clipboard" > Copy item path</ button ></ h1 >< rustdoc-toolbar ></ rustdoc-toolbar >< span class = "sub-heading" >< a class = "src" href = "../src/quote/format.rs.html#111-125" > Source</ a > </ span ></ div >< pre class = "rust item-decl" >< code > macro_rules! format_ident {
2025-01-24 08:59:28 +00:00
($fmt:expr) => { ... };
($fmt:expr, $($rest:tt)*) => { ... };
}</ code ></ pre >< details class = "toggle top-doc" open >< summary class = "hideme" >< span > Expand description</ span ></ summary >< div class = "docblock" >< p > Formatting macro for constructing < code > Ident</ code > s.</ p >
2021-02-19 22:15:49 +00:00
< br >
2024-12-12 15:26:31 +00:00
< h2 id = "syntax" >< a class = "doc-anchor" href = "#syntax" > §</ a > Syntax</ h2 >
2025-08-20 14:48:48 +00:00
< p > Syntax is copied from the < a href = "https://doc.rust-lang.org/1.89.0/alloc/macro.format.html" title = "macro alloc::format" >< code > format!</ code ></ a > macro, supporting both positional and
2021-02-19 22:15:49 +00:00
named arguments.</ p >
< p > Only a limited set of formatting traits are supported. The current mapping
of format types to traits is:</ p >
< ul >
2023-11-22 13:48:01 +00:00
< li >< code > {}</ code > ⇒ < a href = "trait.IdentFragment.html" title = "trait quote::IdentFragment" >< code > IdentFragment</ code ></ a ></ li >
2025-08-20 14:48:48 +00:00
< li >< code > {:o}</ code > ⇒ < a href = "https://doc.rust-lang.org/1.89.0/core/fmt/trait.Octal.html" title = "trait core::fmt::Octal" >< code > Octal</ code ></ a ></ li >
< li >< code > {:x}</ code > ⇒ < a href = "https://doc.rust-lang.org/1.89.0/core/fmt/trait.LowerHex.html" title = "trait core::fmt::LowerHex" >< code > LowerHex</ code ></ a ></ li >
< li >< code > {:X}</ code > ⇒ < a href = "https://doc.rust-lang.org/1.89.0/core/fmt/trait.UpperHex.html" title = "trait core::fmt::UpperHex" >< code > UpperHex</ code ></ a ></ li >
< li >< code > {:b}</ code > ⇒ < a href = "https://doc.rust-lang.org/1.89.0/core/fmt/trait.Binary.html" title = "trait core::fmt::Binary" >< code > Binary</ code ></ a ></ li >
2021-02-19 22:15:49 +00:00
</ ul >
2025-08-20 14:48:48 +00:00
< p > See < a href = "https://doc.rust-lang.org/1.89.0/alloc/fmt/index.html" title = "mod alloc::fmt" >< code > std::fmt</ code ></ a > for more information.</ p >
2021-02-19 22:15:49 +00:00
< br >
2024-12-12 15:26:31 +00:00
< h2 id = "identfragment" >< a class = "doc-anchor" href = "#identfragment" > §</ a > IdentFragment</ h2 >
2023-11-22 13:48:01 +00:00
< p > Unlike < code > format!</ code > , this macro uses the < a href = "trait.IdentFragment.html" title = "trait quote::IdentFragment" >< code > IdentFragment</ code ></ a > formatting trait by
2021-02-19 22:15:49 +00:00
default. This trait is like < code > Display</ code > , with a few differences:</ p >
< ul >
< li >< code > IdentFragment</ code > is only implemented for a limited set of types, such as
unsigned integers and strings.</ li >
2023-11-22 13:48:01 +00:00
< li >< a href = "../proc_macro2/struct.Ident.html" title = "struct proc_macro2::Ident" >< code > Ident</ code ></ a > arguments will have their < code > r#</ code > prefixes stripped, if present.</ li >
2021-02-19 22:15:49 +00:00
</ ul >
< br >
2024-12-12 15:26:31 +00:00
< h2 id = "hygiene" >< a class = "doc-anchor" href = "#hygiene" > §</ a > Hygiene</ h2 >
2023-11-22 13:48:01 +00:00
< p > The < a href = "../proc_macro2/struct.Span.html" title = "struct proc_macro2::Span" >< code > Span</ code ></ a > of the first < code > Ident</ code > argument is used as the span of the final
identifier, falling back to < a href = "../proc_macro2/struct.Span.html#method.call_site" title = "associated function proc_macro2::Span::call_site" >< code > Span::call_site</ code ></ a > when no identifiers are
2021-02-19 22:15:49 +00:00
provided.</ p >
2023-02-07 01:24:14 +00:00
< div class = "example-wrap" >< pre class = "rust rust-example-rendered" >< code >< span class = "comment" > // If `ident` is an Ident, the span of `my_ident` will be inherited from it.
2024-12-12 15:26:31 +00:00
</ span >< span class = "kw" > let </ span > my_ident = < span class = "macro" > format_ident!</ span > (< span class = "string" > "My{}{}"</ span > , ident, < span class = "string" > "IsCool"</ span > );
< span class = "macro" > assert_eq!</ span > (my_ident, < span class = "string" > "MyIdentIsCool"</ span > );</ code ></ pre ></ div >
2021-02-19 22:15:49 +00:00
< p > Alternatively, the span can be overridden by passing the < code > span</ code > named
argument.</ p >
2023-02-07 01:24:14 +00:00
< div class = "example-wrap" >< pre class = "rust rust-example-rendered" >< code >< span class = "kw" > let </ span > my_span = < span class = "comment" > /* ... */</ span > ;
2024-12-12 15:26:31 +00:00
< span class = "macro" > format_ident!</ span > (< span class = "string" > "MyIdent"</ span > , span = my_span);</ code ></ pre ></ div >
2021-02-19 22:15:49 +00:00
< p >< br ></ p >
2024-12-12 15:26:31 +00:00
< h2 id = "panics" >< a class = "doc-anchor" href = "#panics" > §</ a > Panics</ h2 >
2021-02-19 22:15:49 +00:00
< p > This method will panic if the resulting formatted string is not a valid
identifier.</ p >
< br >
2024-12-12 15:26:31 +00:00
< h2 id = "examples" >< a class = "doc-anchor" href = "#examples" > §</ a > Examples</ h2 >
2021-02-19 22:15:49 +00:00
< p > Composing raw and non-raw identifiers:</ p >
2024-12-12 15:26:31 +00:00
< div class = "example-wrap" >< pre class = "rust rust-example-rendered" >< code >< span class = "kw" > let </ span > my_ident = < span class = "macro" > format_ident!</ span > (< span class = "string" > "My{}"</ span > , < span class = "string" > "Ident"</ span > );
< span class = "macro" > assert_eq!</ span > (my_ident, < span class = "string" > "MyIdent"</ span > );
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > raw = < span class = "macro" > format_ident!</ span > (< span class = "string" > "r#Raw"</ span > );
< span class = "macro" > assert_eq!</ span > (raw, < span class = "string" > "r#Raw"</ span > );
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > my_ident_raw = < span class = "macro" > format_ident!</ span > (< span class = "string" > "{}Is{}"</ span > , my_ident, raw);
< span class = "macro" > assert_eq!</ span > (my_ident_raw, < span class = "string" > "MyIdentIsRaw"</ span > );</ code ></ pre ></ div >
2021-02-19 22:15:49 +00:00
< p > Integer formatting options:</ p >
2023-02-07 01:24:14 +00:00
< div class = "example-wrap" >< pre class = "rust rust-example-rendered" >< code >< span class = "kw" > let </ span > num: u32 = < span class = "number" > 10</ span > ;
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > decimal = < span class = "macro" > format_ident!</ span > (< span class = "string" > "Id_{}"</ span > , num);
< span class = "macro" > assert_eq!</ span > (decimal, < span class = "string" > "Id_10"</ span > );
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > octal = < span class = "macro" > format_ident!</ span > (< span class = "string" > "Id_{:o}"</ span > , num);
< span class = "macro" > assert_eq!</ span > (octal, < span class = "string" > "Id_12"</ span > );
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > binary = < span class = "macro" > format_ident!</ span > (< span class = "string" > "Id_{:b}"</ span > , num);
< span class = "macro" > assert_eq!</ span > (binary, < span class = "string" > "Id_1010"</ span > );
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > lower_hex = < span class = "macro" > format_ident!</ span > (< span class = "string" > "Id_{:x}"</ span > , num);
< span class = "macro" > assert_eq!</ span > (lower_hex, < span class = "string" > "Id_a"</ span > );
2021-02-19 22:15:49 +00:00
2024-12-12 15:26:31 +00:00
< span class = "kw" > let </ span > upper_hex = < span class = "macro" > format_ident!</ span > (< span class = "string" > "Id_{:X}"</ span > , num);
< span class = "macro" > assert_eq!</ span > (upper_hex, < span class = "string" > "Id_A"</ span > );</ code ></ pre ></ div >
2023-11-22 13:48:01 +00:00
</ div ></ details ></ section ></ div ></ main ></ body ></ html >