How do I call a function through a member variable?

后端 未结 1 993
后悔当初
后悔当初 2020-11-29 13:29

Toying with Rust, I\'m extracting some code into a class. To keep it self-contained but separate functionality, I want to hang onto a callback function and call it later.

相关标签:
1条回答
  • 2020-11-29 13:40

    foo.bar(...) is always parsed as a method call, it never looks for fields. This avoids ambiguity, especially with traits. One can force it to be a field access by separating the call and the field access into two distinct expressions, for example,

    let f = self.go;
    f(n)
    

    Or, better, just (self.go)(n).

    Issue #2392 covers improving these diagnostics.

    0 讨论(0)
提交回复
热议问题