0

code copied verbatim from the rust Pin documentation https://doc.rust-lang.org/std/pin/macro.pin.html#with-generators. what does static preceding the || mean?

fn generator_fn() -> impl Generator<Yield = usize, Return = ()> /* not Unpin */ {
 // Allow generator to be self-referential (not `Unpin`)
 // vvvvvv        so that locals can cross yield points.
    static || {
        let foo = String::from("foo");
        let foo_ref = &foo; // ------+
        yield 0;                  // | <- crosses yield point!
        println!("{foo_ref}"); // <--+
        yield foo.len();
    }
}
ajp
  • 1,723
  • 14
  • 22
  • Does this answer your question? [What does a closure prefixed with `static` mean? And when would I use it?](/q/75984277/2189130) Its almost the same question (and may answers it tangentially) though if the question here focuses on generators with `static` vs those without it'd probably be better as a separate answer here and not closed as a dupe. I'd let Chayim Friedman promote his comment there into a complete answer here if available to. – kmdreko Jul 24 '23 at 02:59
  • 1
    Does this answer your question? [What does a closure prefixed with \`static\` mean? And when would I use it?](https://stackoverflow.com/questions/75984277/what-does-a-closure-prefixed-with-static-mean-and-when-would-i-use-it) – al3x Jul 24 '23 at 03:19
  • @kmdreko @al3x I might be blind, but that answer does not mention what `static` in that context actually means. – Ivan C Jul 24 '23 at 05:00
  • @IvanC read [Chaiym Friedman's comment](https://stackoverflow.com/questions/75984277/what-does-a-closure-prefixed-with-static-mean-and-when-would-i-use-it#comment134018216_75984478). – cafce25 Jul 24 '23 at 05:39
  • ```Allow generator to be self-referential (not `Unpin`)``` the code you quoted explains the `static` very clearly. – Chayim Friedman Jul 24 '23 at 11:38
  • @ChayimFriedman ah fair enough, now I realize what the "vvvvvv" in the source comment means. I was wondering :) – ajp Jul 24 '23 at 16:10
  • @kmdreko the comment on the answer to that question answer this question, but the title is misleading (it's not a closure, despite the syntactic similarity) – ajp Jul 24 '23 at 16:14
  • @ajp I agree, which is why I'd prefer an answer here to explicitly state that rather than a comment in a tangential question. – kmdreko Jul 24 '23 at 16:18

0 Answers0