diff --git a/posts/2021-07-29-Rust-1.54.0.md b/posts/2021-07-29-Rust-1.54.0.md index aae548ea8..7bfa75966 100644 --- a/posts/2021-07-29-Rust-1.54.0.md +++ b/posts/2021-07-29-Rust-1.54.0.md @@ -33,11 +33,29 @@ Rust 1.54 supports invoking function-like macros inside attributes. Function-lik #![doc = include_str!("README.md")] ``` -Macros can be nested inside the attribute as well, for example to include content generated by a build script: +Macros can be nested inside the attribute as well. +For example, the `concat!` macro can be used to construct a doc comment from within a macro that uses `stringify!` to include substitutions: ```rust -#[path = concat!(env!("OUT_DIR"), "/generated.rs")] -mod generated; +macro_rules! make_function { + ($name:ident, $value:expr) => { + #[doc = concat!("The `", stringify!($name), "` example.")] + /// + /// # Example + /// + /// ``` + #[doc = concat!( + "assert_eq!(", module_path!(), "::", stringify!($name), "(), ", + stringify!($value), ");") + ] + /// ``` + pub fn $name() -> i32 { + $value + } + }; +} + +make_function! {func_name, 123} ``` Read [here](https://github.com/rust-lang/rust/pull/83366) for more details.