[][src]Macro autodo_helper::format_template_code

format_template_code!() { /* proc-macro */ }

extend format! macro, accept in {…} internal input the rust template code.

扩展 format! 宏, 接受在 {...} 中输入 rust 模板代码.

宏展开后实际上是以命名参数的方式使用 format!(template,named_args...).

结构:

format_template_code!(template,args1_r=value,args2_r=value,...) -> String

参数:

template: 以大括号 {...} 包围的模板代码, 在模板中以 {var_name} 的方式指定命名替换参数;
arg,... : 命名替换参数, 以 arg_1 = "user",arg_2 = "18",... 的方式指定替换值;

示例:

mod format_template_code_example {

    use autodo_helper::format_template_code;

    #[test]
    fn test() {
        let s = format_template_code!(
            /* in {...} internal input template code */
            {
                struct {struct_name} {
                    a: String,
                    b: String,
                }
                macro_rules! {macro_name} {
                    () => {
                        println!("do something...");
                    };
                }
            },
            struct_name = "Example",
            macro_name = "example_macro",
        );

        assert_eq!(
            s,
            r#"struct Example  { a:String,b:String, } macro_rules! example_macro  { ()=> { println!("do something..."); } ; } "#
        );
    }
    // format s value as follows:
    struct Example {
        a: String,
        b: String,
    }
    macro_rules! example_macro {
        () => {
            println!("do something...");
        };
    }
}