2

I am trying to understand the following code in actix-web

pub fn resource<F, R>(self, path: &str, f: F) -> App<S>
where
    F: FnOnce(&mut Resource<S>) -> R + 'static,

From my understanding, resource is a function that takes 2 parameters: a string slice and a function.

The function can be used only once and accepts a mutable reference of Resource with S inside and returns R for which R and S is completely arbitrary.

What is R + static?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
user10714010
  • 865
  • 2
  • 13
  • 20

1 Answers1

4

R is the return value of f.

If you look inside the actix-web source, you can see that this value is discarded/not used. I think this is mostly a convenience thing; rather than specifying a () return value, actix is making it easy by not caring what you return from your f.

I believe the 'static lifetime should be read as applying to the F rather than the R. That is, the f (usually a closure) should have a static lifetime.

Jacob Brown
  • 7,221
  • 4
  • 30
  • 50
  • 1
    You are right about the last point. The `+` operator in this context combines multiple trait or lifetime bounds. `R` isn't a trait bound, but rather a concrete type. `FnOnce(...) -> R` on the other hand is a trait bound, and `'static` is a lifetime bound, so the two can be combined with `+`. – Sven Marnach Dec 14 '18 at 08:59