Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most idiomatic way to concatenate integer variables?

The compiler doesn't seem to infer that the integer variables are passed as string literals into the concat! macro, so I found the stringify! macro that converts these integer variables into string literals, but this looks ugly:

fn date(year: u8, month: u8, day: u8) -> String
{
    concat!(stringify!(month), "/",
            stringify!(day), "/",
            stringify!(year)).to_string()
}
like image 839
elleciel Avatar asked Dec 09 '22 05:12

elleciel


1 Answers

concat! takes literals and produces a &'static str at compile time. You should use format! for this:

fn date(year: u8, month: u8, day: u8) -> String {
    format!("{}/{}/{}", month, day, year)
}
like image 170
Dogbert Avatar answered May 20 '23 11:05

Dogbert