pub fn display<T>(inner: T) -> Display<T>where
T: Display,
Expand description
Function to build a string literal.
This is an alternative to manually implementing tokens::FormatInto, since it can tokenize anything that implements Display directly.
On the other hand, things implementing tokens::FormatInto have access to the full range of the Tokens api, allowing it to work more efficiently.
ยงExamples
Example showcasing quoted strings when generating Rust.
use genco::prelude::*;
use std::fmt;
struct Foo(());
impl fmt::Display for Foo {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Foo")
}
}
let map = rust::import("std::collections", "HashMap");
let foo = Foo(());
let tokens = quote! {
let mut m = $map::<u32, &str>::new();
m.insert(0, $(display(&foo)));
};
assert_eq!(
vec![
"use std::collections::HashMap;",
"",
"let mut m = HashMap::<u32, &str>::new();",
"m.insert(0, Foo);",
],
tokens.to_file_vec()?,
);