Rust without Macros

How hard would it be to write code in rust without the use of any macros whatsoever?

It probably wouldn't be that hard, but it would be pretty annoying considering some things like string formatting are primarily done through macros in general.

Why do you ask?

2 Likes

Rust is designed to have macros.

Macros take care of boilerplate, enable specialized syntax, and some duck typing. This allows other language features like generics to be stricter, less flexible, and not worry about boilerplate so much. Without macros you'll find that some things can't be done nicely in Rust, and require annoying boilerplate or alternative designs that are more difficult to implement and less usable.

For example, Rust doesn't have a trait for all integer types like u8, u16, u32, u64, and u128. All their methods are implemented using macros. You can write all that code manually 5 or 10 times, or you can try to write that code once through a trait, but you'll find that Rust's traits are not convenient for this.

6 Likes

As far as I know, some macros in core are implemented as compiler built-ins, therefore some things cannot be done without using macros, like format_args!() (which works as an interface to the Rust formatting system and is used by all formatting macros, such as write!(), format!(), print!()). Also, if you use FFI, you may have to to add the output of bindgen into your crate, which is usually done by something like:

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

All macros here are compiler built-ins and used to include the generated bindings file from compiler's output directory into the program source. The OUT_DIR depends on where the source tree is located in the filesystem and therefore cannot be hard-coded, so I see no alternative to using these macros.

2 Likes