Doc comments are translated to attributes of the form
#[doc = "documentation text"]
and can be matched in their translated form:
macro_rules! print_doc {
(#[doc = $doc:expr]) => {
pub fn foo() {
println!($doc);
}
}
}
print_doc!(
/// gloink
);
fn main() {
foo();
}
If you want to emit the doc comment in modified form, you need to use the attribute syntax inside your macro, as explained in this answer:
To concatenate the original doc comment with your annotation, you can use the macro concat!()
from the standard library. However, macro calls are not allowed inside attributes, so you need a helper macro, e.g.
macro_rules! annotated_func {
($doc:expr) => {
#[doc = $doc]
pub fn foo() {
println!($doc);
}
}
}
macro_rules! print_doc {
(#[doc = $doc:expr]) => {
annotated_func!(concat!($doc, "\nannotation"));
}
}