This series serves as a practical (but not-exhaustive) introduction to declarative macro_rules!
. I've put together some Rust macro examples to show how macros can be helpful for improving ergonomics around repetitive or error-prone tasks. The examples cover some great scenarios for macros like:
We previously covered a very basic declarative macro that wrapped some timing logic around a function to be run. This article expands on that with some additional matching techinques to build a retry!
macro to be used like:
let res = retry!(|| { sometimes_fail(10) });
assert!(res.is_ok());
let res = retry!(sometimes_fail, 10; retries = 3);
assert!(res.is_ok());
We've now built a simple retry!
macro where the limited logic is contained in the macro_rules. This article will focus on an improved Retryable
struct (with RetryStrategy
) that we'll then build a new macro to simplify instantation for.
Forgoing macros for a bit, let's setup some retry structs and implementations. First is a Retryable
struct to contain our function/closure to retry, and a RetryStrategy
with options for retrying (number of retries, delay, etc.):