Macro tutorial recurrence! not work

macro_rules! recurrence {
    ( a[n]: $sty:ty = $($inits:expr),+ ... $recur:expr ) => {
        /* ... */
    };
}

fn main() {
    let fib = recurrence![a[n]: u64 = 0, 1 ... a[n-1] + a[n-2]];

    for e in fib.take(10) { println!("{}", e) }
}

im learning macro tutorial:
https://danielkeep.github.io/quick-intro-to-macros.html
can not compile this macro,please help

Hey, look at the note in the begenning of page:)

Note : This article is for an obsolete version of Rust, and has been superceded by the slightly less misleadingly named "A Practical Intro to Macros in Rust 1.0".

1 Like

It won't compile because the body of the macro was left out. See the /* ... */?

thank you
i tried https://danielkeep.github.io/practical-intro-to-macros.html,still not work.
$seq:ident [ $ind:ident ]: $sty:ty = $($inits:expr),+ ... $recur:expr
^^^not allowed after 'expr' fragments

I assume the error is pointing at the ...? That would make sense, because the guide appears to have been written before macro_rules was stabilized, and the future-proofing rules (that forbid e.g. ... after expr) were added in the same 1.0.0-alpha release.

So yes, it is because the guide is old. Here is the new version:

https://danielkeep.github.io/tlborm/book/pim-README.html

according to https://github.com/rust-lang/rust/issues/25658
... is the problem,but i dont know how to fix it

Simply put, you can't fix something that's there for macro hygiene. You'll have to work around it.

Something that would work similarly with a delimiter:

macro_rules! recurrence {
    (a[n]: $sty:ty = ($($inits:expr),+) ... $recur:expr) => {
        /**/
    };
}
macro_rules! recurrence {
    (a[n]: $sty:ty = ($($inits:expr),+) ... $recur:expr) => {
        /**/
    };
}

fn main() {
    recurrence![ a[n]: u64 = (0, 1) ... a[n-1] + a[n-2] ];
}

it works,but not natural:
a[n]: u64 = 0, 1 ... a[n-1] + a[n-2]

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