1

What is wrong with this code?

fn launch_thread<T>(_transform: T)
where
    T: fn(&String) -> String,
{
}

Playground

When I introduce the where clause, it stops working:

error: expected one of `!`, `(`, `,`, `?`, `for`, `{`, lifetime, or path, found keyword `fn`
 --> src/lib.rs:3:8
  |
3 |     T: fn(&String) -> String,
  |        ^^ expected one of 8 possible tokens
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Nulik
  • 6,748
  • 10
  • 60
  • 129
  • `fn` is the keyword and the type, you are referring to `std::ops::Fn`, or just `Fn`. `where T: Fn(&String) -> String + Send + 'static` will work. – user2722968 Feb 05 '20 at 14:25
  • @user2722968 , well it says in the docs: `This trait (Fn) is not to be confused with function pointers (fn).` , and what I want to do is to exactly that: *pass a function pointer* – Nulik Feb 05 '20 at 14:28
  • Does this answer your question? [Differences between \`fn\` and \`||\` in type for an array of functions](https://stackoverflow.com/questions/24979454/differences-between-fn-and-in-type-for-an-array-of-functions) TL;DR If you want to use function references, you can do that: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=24176c7856346ba35cf871744b64f47f – Boiethios Feb 05 '20 at 14:29
  • @Boiethios great link, but no, it doesn't answer. This is something with `where` syntax. Because if you uncomment the commented code block, everything is going to work. – Nulik Feb 05 '20 at 14:39
  • 1
    @Nulik `fn` isn't a trait, so there is no point to use it as a where clause. – Boiethios Feb 05 '20 at 14:45
  • @Boiethios , now everything is clear. – Nulik Feb 05 '20 at 14:48

0 Answers0