Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 31, 2019 17:01
Show Gist options
  • Save rust-play/816d3f6c36ae0cdf0a728b693f4940f5 to your computer and use it in GitHub Desktop.
Save rust-play/816d3f6c36ae0cdf0a728b693f4940f5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
macro_rules! fmt_args {
($($tt:tt)*) => {
_fmt_args_!("",; $($tt)*)
};
}
macro_rules! _fmt_args_ {
($fmt:expr, $($args:expr,)*; ) => {
format_args!($fmt $(,$args)*)
};
($fmt:expr, $($args:expr,)*; $text:literal $($tail:tt)*) => {
_fmt_args_!(concat!($fmt, $text), $($args,)*; $($tail)*)
};
($fmt:expr, $($args:expr,)*; {$e:expr} $($tail:tt)*) => {
_fmt_args_!(concat!($fmt, "{}"), $($args,)* $e,; $($tail)*)
};
($fmt:expr, $($args:expr,)*; {$e:expr; $($s:tt)*} $($tail:tt)*) => {
_fmt_args_!(concat!($fmt, "{:", $(stringify!($s),)* "}"), $($args,)* $e,; $($tail)*)
};
}
fn main() {
let user = "username";//some_other_module::get_user();
let port = 80;//some_other_module::get_port();
let url = fmt_args!("https://"{user}"@www.website.com:"{port}"/").to_string();
println!("{}", url);
assert_eq!(url, "https://[email protected]:80/");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment