Hi all,
I'm trying to get the following macros working. They work fine unless I throw in a format! macro instead of a string literal.
macro_rules! exit {
($msg:expr) => {
println!($msg);
std::process::exit(1);
};
}
macro_rules! none_exit {
($fcall:expr, $msg:expr) => {
match $fcall {
Some(r) => r,
None => {
exit!($msg);
}
}
};
}
fn main() {
// Works
none_exit!(None, "Something terrible has happened ...");
// Fails
none_exit!(None, format!("Something terrible has happened in module {} ...", "X"));
}
I tried quite a few things but I can't get around this error message.
error: format argument must be a string literal
--> src/main.rs:25:22
|
25 | none_exit!(None, format!("Something terrible has happened in module {} ...", "X"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might be missing a string literal to format with
|
25 | none_exit!(None, "{}", format!("Something terrible has happened in module {} ...", "X"));
| +++++
Any ideas how to solve this?
Thank you in advance.