Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of 'static as a function constraint?

Tags:

rust

lifetime

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 marking global_variable static is similar to what static means ie. in c#, space for the variable is allocated in a separate segment of the memory, existing for the whole execution of the program.

static global_variable: i32 = 5;

But what I don't understand what giving 'static constraint means. Is kate somehow promoted, and her lifetime extended so it now lives for the whole execution of program too?

Or does it simply mean it'll be deallocated as soon as foo stops using it?

like image 436
jojo Avatar asked May 28 '15 07:05

jojo


People also ask

What is a static constraint?

Just like static variables in a class, constraints can be declared as static . A static constraint is shared across all the class instances. Constraints are affected by the static keyword only if they are turned on and off using constraint_mode() method.

What is static constraints in Java?

Method's behavior is not dependent at all on instance variables. Such method can be declared static, which means no instance of the class is required to run the method.

What is static constraints in System Verilog?

Static Constraints in SystemVerilog A constraint block can be defined as static by including the static keyword in its definition. constraint block with the static keyword followed by constraint keyword is called as a static constraint. the static constraint is shared across all the class instances.


1 Answers

Putting a constraint like T: 'a means that all lifetime parameters of the type T must satisfy the lifetime constraint 'a (thus, must outlive it).

For example, if I have this struct:

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 struct without any lifetime parameter will always satisfy any lifetime constraint.

So as a summary, a constraint like F: 'static means that either:

  • F has no lifetime parameter
  • all lifetime parameters of F are 'static
like image 89
Levans Avatar answered Sep 21 '22 19:09

Levans