1

in Rocket documentation I found this construction

request.guard::<&State<MyConfig>>().await

I understand await and & but this func::<type>() part is completely different from what I have seen in other mainstream languages func() like C++, C#, Java, python... probably best answer would be to point me to the right reading material about it because it's not framework specific

quester
  • 534
  • 5
  • 18
  • 2
    [Generics](https://doc.rust-lang.org/book/ch10-01-syntax.html). More precisely you call a function this way when the compiler can't directly infer the type. Now, please, this question is much too wide. – Denys Séguret Jun 08 '21 at 08:20
  • 2
    Not so different from C++ - if you define a function as `template func() { ... }`, you can call it as `func()`. This is the same thing, except the type is `&State`. It explicitly specifies the type for the generic function, presumably because the type could not be inferred from function arguments (as the function accepts no arguments). The `::<...>` operator is sometimes referred to as [the turbofish](https://techblog.tonsser.com/posts/what-is-rusts-turbofish). – user4815162342 Jun 08 '21 at 08:21

1 Answers1

5

It is called the turbofish, and is used for generics - and sometimes used when the Rust compiler cannot infer the type of some variable, for specifying the concrete type of that variable.

See non-operator symbols on the Rust Book for more information.

  • Welcome to Stack Overflow! This is a good answer. Adding a link to the docs (https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols) would bring it up to a _great_ answer. – Noah Bogart Jun 08 '21 at 14:36