Macro Trait Implementation



trait Foo {
    fn bar(self);

fn main() {

We can implement Foo for i32 currently with a macro if we wish as:

macro_rules! impl_bar {
    () => (
        fn bar(self) { return () }

impl Foo for i32 {

But wouldn’t it be cool if we could do something like:

impl Foo for i32 {
    macro bar {
        (...) => (...)



I didn’t see the point here. We create macros to reduce code duplication. In your case, if the macro is only for a single impl I didn’t see how it helps on reducing code.

Your example with a macro defined outside the impl block can be used to implement other trait instances. but your proposed version have the macro inside, makes it only usable inside the body. This limits its usage.

closed #3

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.