连接整数变量最惯用的方法是什么?

What is the most idiomatic way to concatenate integer variables?

编译器似乎没有推断整数变量作为字符串文字传递给 concat! 宏,所以我找到了 stringify! 宏将这些整数变量转换为字符串文字,但这看起来很难看:

fn date(year: u8, month: u8, day: u8) -> String
{
    concat!(stringify!(month), "/",
            stringify!(day), "/",
            stringify!(year)).to_string()
}

concat! 获取文字并在 编译 时生成 &'static str。您应该为此使用 format!

fn date(year: u8, month: u8, day: u8) -> String {
    format!("{}/{}/{}", month, day, year)
}

另请注意,您的示例并没有按照您的意愿行事!当你编译它时,你会收到这些警告:

<anon>:1:9: 1:13 warning: unused variable: `year`, #[warn(unused_variables)] on by default
<anon>:1 fn date(year: u8, month: u8, day: u8) -> String
                 ^~~~
<anon>:1:19: 1:24 warning: unused variable: `month`, #[warn(unused_variables)] on by default
<anon>:1 fn date(year: u8, month: u8, day: u8) -> String
                           ^~~~~
<anon>:1:30: 1:33 warning: unused variable: `day`, #[warn(unused_variables)] on by default
<anon>:1 fn date(year: u8, month: u8, day: u8) -> String
                                      ^~~

请注意,所有变量都未使用!调用该函数的输出将始终为字符串:

month/day/year