Is it possible for a macro to turn an identifier lowercase?

I just wrote a procedural macro (casey) to do this.

#![feature(proc_macro_hygiene)]

use casey::lower;

lower!(B); // would render as `b`

Update

proc_macro_hygiene is stable as of rust 1.45.0, so no longer requires nightly.


The previous answers are all correct; standard declarative macros can't do this, and you can drop to procedural macros instead. However, a simpler alternative to procedural macros (especially if, like myself, that's an area of the language you haven't delved into yet) is dtolnay's paste crate.

An example from those docs:

use paste::paste;

paste! {
    // Defines a const called `QRST`.
    const [<Q R S T>]: &str = "success!";
}

fn main() {
    assert_eq!(
        paste! { [<Q R S T>].len() },
        8,
    );
}

Case conversion is also supported, e.g. [<ld_ $reg:lower _expr>]

Tags:

Macros

Rust