What is the meaning of “static” as a function limitation?

What is the meaning of static in this context?

fn foo<F: Human + 'static>(param: F) {}

 fn main() { let kate = Kate { age: 30 }; foo(kate); } 

I understand that marking global_variable static is similar to what a static facility means. in C #, space for a variable is allocated to a separate memory segment that exists for the entire program execution.

 static global_variable: i32 = 5; 

But I don’t understand what “static restriction” means. Is kate somehow advanced, and has her life span extended so that now she lives for the entire program?

Or does it just mean that it will be released as soon as foo stops using it?

+6
source share
1 answer

Putting a constraint like T: 'a means that all parameters of the lifetime of type T must satisfy the constraint of the lifetime of 'a (so he must survive it).

For example, if I have this structure:

 struct Kate<'a, 'b> { address: &'a str, lastname: &'b str } 

Kate<'a, 'b> will satisfy the constraint F: Human + 'static only if 'a == 'static and 'b == 'static .

However, a structure without any lifetime parameter will always satisfy any limitation of the lifetime.

So, as a summary, a restriction of type F: 'static means either:

  • F has no lifetime parameter
  • all parameters of the lifetime F are equal to 'static
+10
source

Source: https://habr.com/ru/post/988068/


All Articles